ViewsData.php 4.12 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
<?php

namespace Drupal\content_moderation;

use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;

/**
 * Provides the content_moderation views integration.
11 12
 *
 * @internal
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
 */
class ViewsData {

  use StringTranslationTrait;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The moderation information.
   *
   * @var \Drupal\content_moderation\ModerationInformationInterface
   */
  protected $moderationInformation;

  /**
   * Creates a new ViewsData instance.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\content_moderation\ModerationInformationInterface $moderation_information
   *   The moderation information.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, ModerationInformationInterface $moderation_information) {
    $this->entityTypeManager = $entity_type_manager;
    $this->moderationInformation = $moderation_information;
  }

  /**
   * Returns the views data.
   *
   * @return array
   *   The views data.
   */
  public function getViewsData() {
    $data = [];

54
    $entity_types_with_moderation = array_filter($this->entityTypeManager->getDefinitions(), function (EntityTypeInterface $type) {
55
      return $this->moderationInformation->isModeratedEntityType($type);
56 57
    });

58 59
    // Provides a relationship from moderated entity to its moderation state
    // entity.
60
    $content_moderation_state_entity_type = $this->entityTypeManager->getDefinition('content_moderation_state');
61 62
    $content_moderation_state_entity_base_table = $content_moderation_state_entity_type->getDataTable() ?: $content_moderation_state_entity_type->getBaseTable();
    $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 65 66 67 68 69 70 71 72 73
      $table = $entity_type->getDataTable() ?: $entity_type->getBaseTable();

      $data[$table]['moderation_state'] = [
        'title' => t('Moderation state'),
        'relationship' => [
          'id' => 'standard',
          'label' => $this->t('@label moderation state', ['@label' => $entity_type->getLabel()]),
          'base' => $content_moderation_state_entity_base_table,
          'base field' => 'content_entity_id',
          'relationship field' => $entity_type->getKey('id'),
74
          'extra' => [
75 76 77 78 79 80
            [
              'field' => 'content_entity_type_id',
              'value' => $entity_type_id,
            ],
          ],
        ],
81
        'field' => [
82
          'id' => 'moderation_state_field',
83 84 85
          'default_formatter' => 'content_moderation_state',
          'field_name' => 'moderation_state',
        ],
86
        'filter' => ['id' => 'moderation_state_filter', 'allow empty' => TRUE],
87
        'sort' => ['id' => 'moderation_state_sort'],
88 89 90 91 92 93 94 95 96 97 98
      ];

      $revision_table = $entity_type->getRevisionDataTable() ?: $entity_type->getRevisionTable();
      $data[$revision_table]['moderation_state'] = [
        'title' => t('Moderation state'),
        'relationship' => [
          'id' => 'standard',
          'label' => $this->t('@label moderation state', ['@label' => $entity_type->getLabel()]),
          'base' => $content_moderation_state_entity_revision_base_table,
          'base field' => 'content_entity_revision_id',
          'relationship field' => $entity_type->getKey('revision'),
99
          'extra' => [
100 101 102 103 104 105
            [
              'field' => 'content_entity_type_id',
              'value' => $entity_type_id,
            ],
          ],
        ],
106
        'field' => [
107
          'id' => 'moderation_state_field',
108 109 110
          'default_formatter' => 'content_moderation_state',
          'field_name' => 'moderation_state',
        ],
111
        'filter' => ['id' => 'moderation_state_filter', 'allow empty' => TRUE],
112
        'sort' => ['id' => 'moderation_state_sort'],
113 114 115 116 117 118 119
      ];
    }

    return $data;
  }

}