3 namespace Drupal\Core\Plugin\Context;
5 use Drupal\Core\Entity\ContentEntityStorageInterface;
6 use Drupal\Core\Entity\EntityInterface;
7 use Drupal\Core\Entity\EntityTypeInterface;
8 use Drupal\Core\Entity\Plugin\DataType\EntityAdapter;
9 use Drupal\Core\Entity\Plugin\Validation\Constraint\BundleConstraint;
10 use Drupal\Core\Entity\TypedData\EntityDataDefinition;
13 * Defines a class to provide entity context definitions.
15 class EntityContextDefinition extends ContextDefinition {
20 public function __construct($data_type = 'any', $label = NULL, $required = TRUE, $multiple = FALSE, $description = NULL, $default_value = NULL) {
21 // Prefix the data type with 'entity:' so that this class can be constructed
22 // like so: new EntityContextDefinition('node')
23 if (strpos($data_type, 'entity:') !== 0) {
24 $data_type = "entity:$data_type";
26 parent::__construct($data_type, $label, $required, $multiple, $description, $default_value);
30 * Returns the entity type ID of this context.
35 protected function getEntityTypeId() {
36 // The data type is the entity type ID prefixed by 'entity:' (7 characters).
37 return substr($this->getDataType(), 7);
43 protected function getConstraintObjects() {
44 if (!$this->getConstraint('EntityType')) {
45 $this->addConstraint('EntityType', [
46 'type' => $this->getEntityTypeId(),
49 return parent::getConstraintObjects();
55 protected function getSampleValues() {
56 // Get the constraints from the context's definition.
57 $constraints = $this->getConstraintObjects();
58 $entity_type_manager = \Drupal::entityTypeManager();
59 $entity_type_id = $this->getEntityTypeId();
60 $entity_type = $entity_type_manager->getDefinition($entity_type_id);
61 $storage = $entity_type_manager->getStorage($entity_type_id);
62 // If the storage can generate a sample entity we might delegate to that.
63 if ($storage instanceof ContentEntityStorageInterface) {
64 if (!empty($constraints['Bundle']) && $constraints['Bundle'] instanceof BundleConstraint) {
65 foreach ($constraints['Bundle']->getBundleOption() as $bundle) {
66 // We have a bundle, we are bundleable and we can generate a sample.
67 $values = [$entity_type->getKey('bundle') => $bundle];
68 yield EntityAdapter::createFromEntity($storage->create($values));
74 // Either no bundle, or not bundleable, so generate an entity adapter.
75 $definition = EntityDataDefinition::create($entity_type_id);
76 yield new EntityAdapter($definition);
80 * Creates a context definition from a given entity type ID.
82 * @param string $entity_type_id
83 * The entity type ID from which to derive a context definition.
87 public static function fromEntityTypeId($entity_type_id) {
88 $entity_type = \Drupal::entityTypeManager()->getDefinition($entity_type_id);
89 return static::fromEntityType($entity_type);
93 * Creates a context definition from a given entity type.
95 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
96 * The entity type from which to derive a context definition.
100 public static function fromEntityType(EntityTypeInterface $entity_type) {
101 return new static('entity:' . $entity_type->id(), $entity_type->getLabel());
105 * Creates a context definition from a given entity object.
107 * @param \Drupal\Core\Entity\EntityInterface $entity
108 * The entity from which to derive a context definition.
112 public static function fromEntity(EntityInterface $entity) {
113 return static::fromEntityType($entity->getEntityType());