"Fossies" - the Fresh Open Source Software Archive

Member "drupal-8.9.10/core/lib/Drupal/Core/Plugin/Context/EntityContextDefinition.php" (26 Nov 2020, 3889 Bytes) of package /linux/www/drupal-8.9.10.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "EntityContextDefinition.php" see the Fossies "Dox" file reference documentation.

    1 <?php
    2 
    3 namespace Drupal\Core\Plugin\Context;
    4 
    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;
   11 
   12 /**
   13  * Defines a class to provide entity context definitions.
   14  */
   15 class EntityContextDefinition extends ContextDefinition {
   16 
   17   /**
   18    * {@inheritdoc}
   19    */
   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";
   25     }
   26     parent::__construct($data_type, $label, $required, $multiple, $description, $default_value);
   27   }
   28 
   29   /**
   30    * Returns the entity type ID of this context.
   31    *
   32    * @return string
   33    *   The entity type ID.
   34    */
   35   protected function getEntityTypeId() {
   36     // The data type is the entity type ID prefixed by 'entity:' (7 characters).
   37     return substr($this->getDataType(), 7);
   38   }
   39 
   40   /**
   41    * {@inheritdoc}
   42    */
   43   protected function getConstraintObjects() {
   44     if (!$this->getConstraint('EntityType')) {
   45       $this->addConstraint('EntityType', [
   46         'type' => $this->getEntityTypeId(),
   47       ]);
   48     }
   49     return parent::getConstraintObjects();
   50   }
   51 
   52   /**
   53    * {@inheritdoc}
   54    */
   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));
   69         }
   70         return;
   71       }
   72     }
   73 
   74     // Either no bundle, or not bundleable, so generate an entity adapter.
   75     $definition = EntityDataDefinition::create($entity_type_id);
   76     yield new EntityAdapter($definition);
   77   }
   78 
   79   /**
   80    * Creates a context definition from a given entity type ID.
   81    *
   82    * @param string $entity_type_id
   83    *   The entity type ID from which to derive a context definition.
   84    *
   85    * @return static
   86    */
   87   public static function fromEntityTypeId($entity_type_id) {
   88     $entity_type = \Drupal::entityTypeManager()->getDefinition($entity_type_id);
   89     return static::fromEntityType($entity_type);
   90   }
   91 
   92   /**
   93    * Creates a context definition from a given entity type.
   94    *
   95    * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
   96    *   The entity type from which to derive a context definition.
   97    *
   98    * @return static
   99    */
  100   public static function fromEntityType(EntityTypeInterface $entity_type) {
  101     return new static('entity:' . $entity_type->id(), $entity_type->getLabel());
  102   }
  103 
  104   /**
  105    * Creates a context definition from a given entity object.
  106    *
  107    * @param \Drupal\Core\Entity\EntityInterface $entity
  108    *   The entity from which to derive a context definition.
  109    *
  110    * @return static
  111    */
  112   public static function fromEntity(EntityInterface $entity) {
  113     return static::fromEntityType($entity->getEntityType());
  114   }
  115 
  116 }