fromEntityType example

        if (empty($extra_fields['display'])) {
          continue;
        }

        foreach ($extra_fields['display'] as $extra_field_id => $extra_field) {
          $derivative = $base_plugin_definition;

          $derivative['category'] = $this->t('@entity fields', ['@entity' => $entity_type_labels[$entity_type_id]]);

          $derivative['admin_label'] = $extra_field['label'];

          $context_definition = EntityContextDefinition::fromEntityType($entity_type)
            ->addConstraint('Bundle', [$bundle_id]);
          $derivative['context_definitions'] = [
            'entity' => $context_definition,
          ];

          $derivative_id = $entity_type_id . PluginBase::DERIVATIVE_SEPARATOR . $bundle_id . PluginBase::DERIVATIVE_SEPARATOR . $extra_field_id;
          $this->derivatives[$derivative_id] = $derivative;
        }
      }
    }
    return $this->derivatives;
  }
/** * Creates a context definition from a given entity type ID. * * @param string $entity_type_id * The entity type ID from which to derive a context definition. * * @return static */
  public static function fromEntityTypeId($entity_type_id) {
    $entity_type = \Drupal::entityTypeManager()->getDefinition($entity_type_id);
    return static::fromEntityType($entity_type);
  }

  /** * Creates a context definition from a given entity type. * * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type * The entity type from which to derive a context definition. * * @return static */
  public static function fromEntityType(EntityTypeInterface $entity_type) {
    
/** * {@inheritdoc} */
  public function getDerivativeDefinitions($base_plugin_definition) {
    foreach ($this->entityTypeManager->getDefinitions() as $entity_type_id => $entity_type) {
      if ($entity_type->hasKey('bundle')) {
        $this->derivatives[$entity_type_id] = $base_plugin_definition;
        $this->derivatives[$entity_type_id]['label'] = $entity_type->getBundleLabel();
        $this->derivatives[$entity_type_id]['provider'] = $entity_type->getProvider();
        $this->derivatives[$entity_type_id]['context_definitions'] = [
          $entity_type_id => EntityContextDefinition::fromEntityType($entity_type),
        ];
      }
    }
    return $this->derivatives;
  }

}

  public static function fromEntityTypeId($entity_type_id$label = NULL) {
    $entity_type = \Drupal::entityTypeManager()->getDefinition($entity_type_id);
    return static::fromEntityType($entity_type$label);
  }

  /** * Gets a context from an entity type. * * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type * Entity type from which a definition will be derived. * @param string $label * (optional) The label of the context. * * @return static */

  public static function providerTestIsSatisfiedBy() {
    $data = [];

    $content = new EntityType(['id' => 'test_content']);
    $config = new EntityType(['id' => 'test_config']);

    // Entities without bundles.     $data['content entity, matching type, no value'] = [
      TRUE,
      EntityContextDefinition::fromEntityType($content),
      EntityContextDefinition::fromEntityType($content),
    ];
    $entity = (new Prophet())->prophesize(ContentEntityInterface::class)->willImplement(\IteratorAggregate::class);
    $entity->getIterator()->willReturn(new \ArrayIterator([]));
    $entity->getCacheContexts()->willReturn([]);
    $entity->getCacheTags()->willReturn([]);
    $entity->getCacheMaxAge()->willReturn(0);
    $entity->getEntityTypeId()->willReturn('test_content');
    $data['content entity, matching type, correct value'] = [
      TRUE,
      EntityContextDefinition::fromEntityType($content),
      
Home | Imprint | This part of the site doesn't use cookies.