3 namespace Drupal\content_moderation;
5 use Drupal\Core\Entity\EntityTypeInterface;
6 use Drupal\Core\Entity\EntityTypeManagerInterface;
7 use Drupal\Core\StringTranslation\StringTranslationTrait;
10 * Provides the content_moderation views integration.
16 use StringTranslationTrait;
19 * The entity type manager.
21 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
23 protected $entityTypeManager;
26 * The moderation information.
28 * @var \Drupal\content_moderation\ModerationInformationInterface
30 protected $moderationInformation;
33 * Creates a new ViewsData instance.
35 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
36 * The entity type manager.
37 * @param \Drupal\content_moderation\ModerationInformationInterface $moderation_information
38 * The moderation information.
40 public function __construct(EntityTypeManagerInterface $entity_type_manager, ModerationInformationInterface $moderation_information) {
41 $this->entityTypeManager = $entity_type_manager;
42 $this->moderationInformation = $moderation_information;
46 * Returns the views data.
51 public function getViewsData() {
54 $entity_types_with_moderation = array_filter($this->entityTypeManager->getDefinitions(), function (EntityTypeInterface $type) {
55 return $this->moderationInformation->canModerateEntitiesOfEntityType($type);
58 // Provides a relationship from moderated entity to its moderation state
60 $content_moderation_state_entity_type = $this->entityTypeManager->getDefinition('content_moderation_state');
61 $content_moderation_state_entity_base_table = $content_moderation_state_entity_type->getDataTable() ?: $content_moderation_state_entity_type->getBaseTable();
62 $content_moderation_state_entity_revision_base_table = $content_moderation_state_entity_type->getRevisionDataTable() ?: $content_moderation_state_entity_type->getRevisionTable();
63 foreach ($entity_types_with_moderation as $entity_type_id => $entity_type) {
64 $table = $entity_type->getDataTable() ?: $entity_type->getBaseTable();
66 $data[$table]['moderation_state'] = [
67 'title' => t('Moderation state'),
70 'label' => $this->t('@label moderation state', ['@label' => $entity_type->getLabel()]),
71 'base' => $content_moderation_state_entity_base_table,
72 'base field' => 'content_entity_id',
73 'relationship field' => $entity_type->getKey('id'),
76 'field' => 'content_entity_type_id',
77 'value' => $entity_type_id,
81 'field' => ['default_formatter' => 'content_moderation_state'],
84 $revision_table = $entity_type->getRevisionDataTable() ?: $entity_type->getRevisionTable();
85 $data[$revision_table]['moderation_state'] = [
86 'title' => t('Moderation state'),
89 'label' => $this->t('@label moderation state', ['@label' => $entity_type->getLabel()]),
90 'base' => $content_moderation_state_entity_revision_base_table,
91 'base field' => 'content_entity_revision_id',
92 'relationship field' => $entity_type->getKey('revision'),
95 'field' => 'content_entity_type_id',
96 'value' => $entity_type_id,
100 'field' => ['default_formatter' => 'content_moderation_state'],