ModerationStateJoinViewsHandlerTrait.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. namespace Drupal\content_moderation\Plugin\views;
  3. use Drupal\views\Views;
  4. /**
  5. * Assist views handler plugins to join to the content_moderation_state entity.
  6. *
  7. * @internal
  8. */
  9. trait ModerationStateJoinViewsHandlerTrait {
  10. /**
  11. * {@inheritdoc}
  12. */
  13. public function ensureMyTable() {
  14. if (!isset($this->tableAlias)) {
  15. $table_alias = $this->query->ensureTable($this->table, $this->relationship);
  16. // Join the moderation states of the content via the
  17. // ContentModerationState field revision table, joining either the entity
  18. // field data or revision table. This allows filtering states against
  19. // either the default or latest revision, depending on the relationship of
  20. // the filter.
  21. $left_entity_type = $this->entityTypeManager->getDefinition($this->getEntityType());
  22. $entity_type = $this->entityTypeManager->getDefinition('content_moderation_state');
  23. $configuration = [
  24. 'table' => $entity_type->getRevisionDataTable(),
  25. 'field' => 'content_entity_revision_id',
  26. 'left_table' => $table_alias,
  27. 'left_field' => $left_entity_type->getKey('revision'),
  28. 'extra' => [
  29. [
  30. 'field' => 'content_entity_type_id',
  31. 'value' => $left_entity_type->id(),
  32. ],
  33. [
  34. 'field' => 'content_entity_id',
  35. 'left_field' => $left_entity_type->getKey('id'),
  36. ],
  37. ],
  38. ];
  39. if ($left_entity_type->isTranslatable()) {
  40. $configuration['extra'][] = [
  41. 'field' => $entity_type->getKey('langcode'),
  42. 'left_field' => $left_entity_type->getKey('langcode'),
  43. ];
  44. }
  45. $join = Views::pluginManager('join')->createInstance('standard', $configuration);
  46. $this->tableAlias = $this->query->addRelationship('content_moderation_state', $join, 'content_moderation_state_field_revision');
  47. }
  48. return $this->tableAlias;
  49. }
  50. }