ViewsData.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace Drupal\content_moderation;
  3. use Drupal\Core\Entity\EntityTypeInterface;
  4. use Drupal\Core\Entity\EntityTypeManagerInterface;
  5. use Drupal\Core\StringTranslation\StringTranslationTrait;
  6. /**
  7. * Provides the content_moderation views integration.
  8. *
  9. * @internal
  10. */
  11. class ViewsData {
  12. use StringTranslationTrait;
  13. /**
  14. * The entity type manager.
  15. *
  16. * @var \Drupal\Core\Entity\EntityTypeManagerInterface
  17. */
  18. protected $entityTypeManager;
  19. /**
  20. * The moderation information.
  21. *
  22. * @var \Drupal\content_moderation\ModerationInformationInterface
  23. */
  24. protected $moderationInformation;
  25. /**
  26. * Creates a new ViewsData instance.
  27. *
  28. * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
  29. * The entity type manager.
  30. * @param \Drupal\content_moderation\ModerationInformationInterface $moderation_information
  31. * The moderation information.
  32. */
  33. public function __construct(EntityTypeManagerInterface $entity_type_manager, ModerationInformationInterface $moderation_information) {
  34. $this->entityTypeManager = $entity_type_manager;
  35. $this->moderationInformation = $moderation_information;
  36. }
  37. /**
  38. * Returns the views data.
  39. *
  40. * @return array
  41. * The views data.
  42. */
  43. public function getViewsData() {
  44. $data = [];
  45. $entity_types_with_moderation = array_filter($this->entityTypeManager->getDefinitions(), function (EntityTypeInterface $type) {
  46. return $this->moderationInformation->canModerateEntitiesOfEntityType($type);
  47. });
  48. // Provides a relationship from moderated entity to its moderation state
  49. // entity.
  50. $content_moderation_state_entity_type = $this->entityTypeManager->getDefinition('content_moderation_state');
  51. $content_moderation_state_entity_base_table = $content_moderation_state_entity_type->getDataTable() ?: $content_moderation_state_entity_type->getBaseTable();
  52. $content_moderation_state_entity_revision_base_table = $content_moderation_state_entity_type->getRevisionDataTable() ?: $content_moderation_state_entity_type->getRevisionTable();
  53. foreach ($entity_types_with_moderation as $entity_type_id => $entity_type) {
  54. $table = $entity_type->getDataTable() ?: $entity_type->getBaseTable();
  55. $data[$table]['moderation_state'] = [
  56. 'title' => t('Moderation state'),
  57. 'relationship' => [
  58. 'id' => 'standard',
  59. 'label' => $this->t('@label moderation state', ['@label' => $entity_type->getLabel()]),
  60. 'base' => $content_moderation_state_entity_base_table,
  61. 'base field' => 'content_entity_id',
  62. 'relationship field' => $entity_type->getKey('id'),
  63. 'extra' => [
  64. [
  65. 'field' => 'content_entity_type_id',
  66. 'value' => $entity_type_id,
  67. ],
  68. ],
  69. ],
  70. 'field' => [
  71. 'id' => 'field',
  72. 'default_formatter' => 'content_moderation_state',
  73. 'field_name' => 'moderation_state',
  74. ],
  75. 'filter' => ['id' => 'moderation_state_filter', 'allow empty' => TRUE],
  76. ];
  77. $revision_table = $entity_type->getRevisionDataTable() ?: $entity_type->getRevisionTable();
  78. $data[$revision_table]['moderation_state'] = [
  79. 'title' => t('Moderation state'),
  80. 'relationship' => [
  81. 'id' => 'standard',
  82. 'label' => $this->t('@label moderation state', ['@label' => $entity_type->getLabel()]),
  83. 'base' => $content_moderation_state_entity_revision_base_table,
  84. 'base field' => 'content_entity_revision_id',
  85. 'relationship field' => $entity_type->getKey('revision'),
  86. 'extra' => [
  87. [
  88. 'field' => 'content_entity_type_id',
  89. 'value' => $entity_type_id,
  90. ],
  91. ],
  92. ],
  93. 'field' => [
  94. 'id' => 'field',
  95. 'default_formatter' => 'content_moderation_state',
  96. 'field_name' => 'moderation_state',
  97. ],
  98. 'filter' => ['id' => 'moderation_state_filter', 'allow empty' => TRUE],
  99. ];
  100. }
  101. return $data;
  102. }
  103. }