EntityContextDefinition.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace Drupal\Core\Plugin\Context;
  3. use Drupal\Core\Entity\ContentEntityStorageInterface;
  4. use Drupal\Core\Entity\EntityInterface;
  5. use Drupal\Core\Entity\EntityTypeInterface;
  6. use Drupal\Core\Entity\Plugin\DataType\EntityAdapter;
  7. use Drupal\Core\Entity\Plugin\Validation\Constraint\BundleConstraint;
  8. use Drupal\Core\Entity\TypedData\EntityDataDefinition;
  9. /**
  10. * Defines a class to provide entity context definitions.
  11. */
  12. class EntityContextDefinition extends ContextDefinition {
  13. /**
  14. * {@inheritdoc}
  15. */
  16. public function __construct($data_type = 'any', $label = NULL, $required = TRUE, $multiple = FALSE, $description = NULL, $default_value = NULL) {
  17. // Prefix the data type with 'entity:' so that this class can be constructed
  18. // like so: new EntityContextDefinition('node')
  19. if (strpos($data_type, 'entity:') !== 0) {
  20. $data_type = "entity:$data_type";
  21. }
  22. parent::__construct($data_type, $label, $required, $multiple, $description, $default_value);
  23. }
  24. /**
  25. * Returns the entity type ID of this context.
  26. *
  27. * @return string
  28. * The entity type ID.
  29. */
  30. protected function getEntityTypeId() {
  31. // The data type is the entity type ID prefixed by 'entity:' (7 characters).
  32. return substr($this->getDataType(), 7);
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. protected function getConstraintObjects() {
  38. if (!$this->getConstraint('EntityType')) {
  39. $this->addConstraint('EntityType', [
  40. 'type' => $this->getEntityTypeId(),
  41. ]);
  42. }
  43. return parent::getConstraintObjects();
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. protected function getSampleValues() {
  49. // Get the constraints from the context's definition.
  50. $constraints = $this->getConstraintObjects();
  51. $entity_type_manager = \Drupal::entityTypeManager();
  52. $entity_type_id = $this->getEntityTypeId();
  53. $entity_type = $entity_type_manager->getDefinition($entity_type_id);
  54. $storage = $entity_type_manager->getStorage($entity_type_id);
  55. // If the storage can generate a sample entity we might delegate to that.
  56. if ($storage instanceof ContentEntityStorageInterface) {
  57. if (!empty($constraints['Bundle']) && $constraints['Bundle'] instanceof BundleConstraint) {
  58. foreach ($constraints['Bundle']->getBundleOption() as $bundle) {
  59. // We have a bundle, we are bundleable and we can generate a sample.
  60. $values = [$entity_type->getKey('bundle') => $bundle];
  61. yield EntityAdapter::createFromEntity($storage->create($values));
  62. }
  63. return;
  64. }
  65. }
  66. // Either no bundle, or not bundleable, so generate an entity adapter.
  67. $definition = EntityDataDefinition::create($entity_type_id);
  68. yield new EntityAdapter($definition);
  69. }
  70. /**
  71. * Creates a context definition from a given entity type ID.
  72. *
  73. * @param string $entity_type_id
  74. * The entity type ID from which to derive a context definition.
  75. *
  76. * @return static
  77. */
  78. public static function fromEntityTypeId($entity_type_id) {
  79. $entity_type = \Drupal::entityTypeManager()->getDefinition($entity_type_id);
  80. return static::fromEntityType($entity_type);
  81. }
  82. /**
  83. * Creates a context definition from a given entity type.
  84. *
  85. * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
  86. * The entity type from which to derive a context definition.
  87. *
  88. * @return static
  89. */
  90. public static function fromEntityType(EntityTypeInterface $entity_type) {
  91. return new static('entity:' . $entity_type->id(), $entity_type->getLabel());
  92. }
  93. /**
  94. * Creates a context definition from a given entity object.
  95. *
  96. * @param \Drupal\Core\Entity\EntityInterface $entity
  97. * The entity from which to derive a context definition.
  98. *
  99. * @return static
  100. */
  101. public static function fromEntity(EntityInterface $entity) {
  102. return static::fromEntityType($entity->getEntityType());
  103. }
  104. }