Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / lib / Drupal / Core / Plugin / Context / EntityContext.php
1 <?php
2
3 namespace Drupal\Core\Plugin\Context;
4
5 use Drupal\Core\Entity\EntityInterface;
6 use Drupal\Core\Entity\EntityTypeInterface;
7
8 /**
9  * Class to provide a specific entity context.
10  */
11 class EntityContext extends Context {
12
13   /**
14    * Gets a context from an entity type ID.
15    *
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.
20    *
21    * @return static
22    */
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);
26   }
27
28   /**
29    * Gets a context from an entity type.
30    *
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.
35    *
36    * @return static
37    */
38   public static function fromEntityType(EntityTypeInterface $entity_type, $label = NULL) {
39     $definition = EntityContextDefinition::fromEntityType($entity_type);
40     if ($label) {
41       $definition->setLabel($label);
42     }
43     return new static($definition);
44   }
45
46   /**
47    * Gets a context object from an entity.
48    *
49    * @param \Drupal\Core\Entity\EntityInterface $entity
50    *   Entity that provides a context.
51    * @param string $label
52    *   (optional) The label of the context.
53    *
54    * @return \Drupal\Core\Plugin\Context\EntityContext
55    */
56   public static function fromEntity(EntityInterface $entity, $label = NULL) {
57     $context = static::fromEntityType($entity->getEntityType(), $label);
58     $context->setContextValue($entity);
59     return $context;
60   }
61
62 }