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.
14 use StringTranslationTrait;
17 * The entity type manager.
19 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
21 protected $entityTypeManager;
24 * The moderation information.
26 * @var \Drupal\content_moderation\ModerationInformationInterface
28 protected $moderationInformation;
31 * Creates a new ViewsData instance.
33 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
34 * The entity type manager.
35 * @param \Drupal\content_moderation\ModerationInformationInterface $moderation_information
36 * The moderation information.
38 public function __construct(EntityTypeManagerInterface $entity_type_manager, ModerationInformationInterface $moderation_information) {
39 $this->entityTypeManager = $entity_type_manager;
40 $this->moderationInformation = $moderation_information;
44 * Returns the views data.
49 public function getViewsData() {
52 $data['content_revision_tracker']['table']['group'] = $this->t('Content moderation (tracker)');
54 $data['content_revision_tracker']['entity_type'] = [
55 'title' => $this->t('Entity type'),
70 $data['content_revision_tracker']['entity_id'] = [
71 'title' => $this->t('Entity ID'),
86 $data['content_revision_tracker']['langcode'] = [
87 'title' => $this->t('Entity language'),
102 $data['content_revision_tracker']['revision_id'] = [
103 'title' => $this->t('Latest revision ID'),
118 $entity_types_with_moderation = array_filter($this->entityTypeManager->getDefinitions(), function (EntityTypeInterface $type) {
119 return $this->moderationInformation->canModerateEntitiesOfEntityType($type);
122 // Add a join for each entity type to the content_revision_tracker table.
123 foreach ($entity_types_with_moderation as $entity_type_id => $entity_type) {
124 /** @var \Drupal\views\EntityViewsDataInterface $views_data */
125 // We need the views_data handler in order to get the table name later.
126 if ($this->entityTypeManager->hasHandler($entity_type_id, 'views_data') && $views_data = $this->entityTypeManager->getHandler($entity_type_id, 'views_data')) {
127 // Add a join from the entity base table to the revision tracker table.
128 $base_table = $views_data->getViewsTableForEntityType($entity_type);
129 $data['content_revision_tracker']['table']['join'][$base_table] = [
130 'left_field' => $entity_type->getKey('id'),
131 'field' => 'entity_id',
134 'field' => 'entity_type',
135 'value' => $entity_type_id,
140 // Some entity types might not be translatable.
141 if ($entity_type->hasKey('langcode')) {
142 $data['content_revision_tracker']['table']['join'][$base_table]['extra'][] = [
143 'field' => 'langcode',
144 'left_field' => $entity_type->getKey('langcode'),
149 // Add a relationship between the revision tracker table to the latest
150 // revision on the entity revision table.
151 $data['content_revision_tracker']['latest_revision__' . $entity_type_id] = [
152 'title' => $this->t('@label latest revision', ['@label' => $entity_type->getLabel()]),
153 'group' => $this->t('@label revision', ['@label' => $entity_type->getLabel()]),
156 'label' => $this->t('@label latest revision', ['@label' => $entity_type->getLabel()]),
157 'base' => $this->getRevisionViewsTableForEntityType($entity_type),
158 'base field' => $entity_type->getKey('revision'),
159 'relationship field' => 'revision_id',
162 'left_field' => 'entity_type',
163 'value' => $entity_type_id,
169 // Some entity types might not be translatable.
170 if ($entity_type->hasKey('langcode')) {
171 $data['content_revision_tracker']['latest_revision__' . $entity_type_id]['relationship']['extra'][] = [
172 'left_field' => 'langcode',
173 'field' => $entity_type->getKey('langcode'),
180 // Provides a relationship from moderated entity to its moderation state
182 $content_moderation_state_entity_type = \Drupal::entityTypeManager()->getDefinition('content_moderation_state');
183 $content_moderation_state_entity_base_table = $content_moderation_state_entity_type->getDataTable() ?: $content_moderation_state_entity_type->getBaseTable();
184 $content_moderation_state_entity_revision_base_table = $content_moderation_state_entity_type->getRevisionDataTable() ?: $content_moderation_state_entity_type->getRevisionTable();
185 foreach ($entity_types_with_moderation as $entity_type_id => $entity_type) {
186 $table = $entity_type->getDataTable() ?: $entity_type->getBaseTable();
188 $data[$table]['moderation_state'] = [
189 'title' => t('Moderation state'),
192 'label' => $this->t('@label moderation state', ['@label' => $entity_type->getLabel()]),
193 'base' => $content_moderation_state_entity_base_table,
194 'base field' => 'content_entity_id',
195 'relationship field' => $entity_type->getKey('id'),
198 'field' => 'content_entity_type_id',
199 'value' => $entity_type_id,
203 'field' => ['default_formatter' => 'content_moderation_state'],
206 $revision_table = $entity_type->getRevisionDataTable() ?: $entity_type->getRevisionTable();
207 $data[$revision_table]['moderation_state'] = [
208 'title' => t('Moderation state'),
211 'label' => $this->t('@label moderation state', ['@label' => $entity_type->getLabel()]),
212 'base' => $content_moderation_state_entity_revision_base_table,
213 'base field' => 'content_entity_revision_id',
214 'relationship field' => $entity_type->getKey('revision'),
217 'field' => 'content_entity_type_id',
218 'value' => $entity_type_id,
222 'field' => ['default_formatter' => 'content_moderation_state'],
230 * Alters the table and field information from hook_views_data().
233 * An array of all information about Views tables and fields, collected from
234 * hook_views_data(), passed by reference.
236 * @see hook_views_data()
238 public function alterViewsData(array &$data) {
239 $entity_types_with_moderation = array_filter($this->entityTypeManager->getDefinitions(), function (EntityTypeInterface $type) {
240 return $this->moderationInformation->canModerateEntitiesOfEntityType($type);
242 foreach ($entity_types_with_moderation as $type) {
243 $data[$type->getRevisionTable()]['latest_revision'] = [
244 'title' => t('Is Latest Revision'),
245 'help' => t('Restrict the view to only revisions that are the latest revision of their entity.'),
246 'filter' => ['id' => 'latest_revision'],
252 * Gets the table of an entity type to be used as revision table in views.
254 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
258 * The revision base table.
260 protected function getRevisionViewsTableForEntityType(EntityTypeInterface $entity_type) {
261 return $entity_type->getRevisionDataTable() ?: $entity_type->getRevisionTable();