3 namespace Drupal\Core\Plugin\Context;
5 use Drupal\Core\Entity\EntityInterface;
6 use Drupal\Core\Entity\EntityTypeInterface;
9 * Class to provide a specific entity context.
11 class EntityContext extends Context {
14 * Gets a context from an entity type ID.
16 * @param string $entity_type_id
17 * Entity type ID from which a definition will be derived.
18 * @param string $label
19 * (optional) The label of the context.
23 public static function fromEntityTypeId($entity_type_id, $label = NULL) {
24 $entity_type = \Drupal::entityTypeManager()->getDefinition($entity_type_id);
25 return static::fromEntityType($entity_type, $label);
29 * Gets a context from an entity type.
31 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
32 * Entity type from which a definition will be derived.
33 * @param string $label
34 * (optional) The label of the context.
38 public static function fromEntityType(EntityTypeInterface $entity_type, $label = NULL) {
39 $definition = EntityContextDefinition::fromEntityType($entity_type);
41 $definition->setLabel($label);
43 return new static($definition);
47 * Gets a context object from an entity.
49 * @param \Drupal\Core\Entity\EntityInterface $entity
50 * Entity that provides a context.
51 * @param string $label
52 * (optional) The label of the context.
54 * @return \Drupal\Core\Plugin\Context\EntityContext
56 public static function fromEntity(EntityInterface $entity, $label = NULL) {
57 $context = static::fromEntityType($entity->getEntityType(), $label);
58 $context->setContextValue($entity);