Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / modules / content_moderation / src / ViewsData.php
1 <?php
2
3 namespace Drupal\content_moderation;
4
5 use Drupal\Core\Entity\EntityTypeInterface;
6 use Drupal\Core\Entity\EntityTypeManagerInterface;
7 use Drupal\Core\StringTranslation\StringTranslationTrait;
8
9 /**
10  * Provides the content_moderation views integration.
11  *
12  * @internal
13  */
14 class ViewsData {
15
16   use StringTranslationTrait;
17
18   /**
19    * The entity type manager.
20    *
21    * @var \Drupal\Core\Entity\EntityTypeManagerInterface
22    */
23   protected $entityTypeManager;
24
25   /**
26    * The moderation information.
27    *
28    * @var \Drupal\content_moderation\ModerationInformationInterface
29    */
30   protected $moderationInformation;
31
32   /**
33    * Creates a new ViewsData instance.
34    *
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.
39    */
40   public function __construct(EntityTypeManagerInterface $entity_type_manager, ModerationInformationInterface $moderation_information) {
41     $this->entityTypeManager = $entity_type_manager;
42     $this->moderationInformation = $moderation_information;
43   }
44
45   /**
46    * Returns the views data.
47    *
48    * @return array
49    *   The views data.
50    */
51   public function getViewsData() {
52     $data = [];
53
54     $entity_types_with_moderation = array_filter($this->entityTypeManager->getDefinitions(), function (EntityTypeInterface $type) {
55       return $this->moderationInformation->canModerateEntitiesOfEntityType($type);
56     });
57
58     // Provides a relationship from moderated entity to its moderation state
59     // entity.
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();
65
66       $data[$table]['moderation_state'] = [
67         'title' => t('Moderation state'),
68         'relationship' => [
69           'id' => 'standard',
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'),
74           'extra' => [
75             [
76               'field' => 'content_entity_type_id',
77               'value' => $entity_type_id,
78             ],
79           ],
80         ],
81         'field' => [
82           'id' => 'field',
83           'default_formatter' => 'content_moderation_state',
84           'field_name' => 'moderation_state',
85         ],
86         'filter' => ['id' => 'moderation_state_filter', 'allow empty' => TRUE],
87       ];
88
89       $revision_table = $entity_type->getRevisionDataTable() ?: $entity_type->getRevisionTable();
90       $data[$revision_table]['moderation_state'] = [
91         'title' => t('Moderation state'),
92         'relationship' => [
93           'id' => 'standard',
94           'label' => $this->t('@label moderation state', ['@label' => $entity_type->getLabel()]),
95           'base' => $content_moderation_state_entity_revision_base_table,
96           'base field' => 'content_entity_revision_id',
97           'relationship field' => $entity_type->getKey('revision'),
98           'extra' => [
99             [
100               'field' => 'content_entity_type_id',
101               'value' => $entity_type_id,
102             ],
103           ],
104         ],
105         'field' => [
106           'id' => 'field',
107           'default_formatter' => 'content_moderation_state',
108           'field_name' => 'moderation_state',
109         ],
110         'filter' => ['id' => 'moderation_state_filter', 'allow empty' => TRUE],
111       ];
112     }
113
114     return $data;
115   }
116
117 }