You are here

class DefaultSelectionDeriver in Drupal 8

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Entity/Plugin/Derivative/DefaultSelectionDeriver.php \Drupal\Core\Entity\Plugin\Derivative\DefaultSelectionDeriver

Provides derivative plugins for the DefaultSelection plugin.

Hierarchy

Expanded class hierarchy of DefaultSelectionDeriver

See also

\Drupal\Core\Entity\Plugin\EntityReferenceSelection\DefaultSelection

\Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginManager

\Drupal\Core\Entity\Annotation\EntityReferenceSelection

\Drupal\Core\Entity\EntityReferenceSelection\SelectionInterface

Plugin API

File

core/lib/Drupal/Core/Entity/Plugin/Derivative/DefaultSelectionDeriver.php, line 20

Namespace

Drupal\Core\Entity\Plugin\Derivative
View source
class DefaultSelectionDeriver extends DeriverBase implements ContainerDeriverInterface {
  use DeprecatedServicePropertyTrait;

  /**
   * {@inheritdoc}
   */
  protected $deprecatedProperties = [
    'entityManager' => 'entity.manager',
  ];

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Creates a DefaultSelectionDeriver object.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager) {
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, $base_plugin_id) {
    return new static($container
      ->get('entity_type.manager'));
  }

  /**
   * {@inheritdoc}
   */
  public function getDerivativeDefinitions($base_plugin_definition) {
    foreach ($this->entityTypeManager
      ->getDefinitions() as $entity_type_id => $entity_type) {
      $this->derivatives[$entity_type_id] = $base_plugin_definition;
      $this->derivatives[$entity_type_id]['entity_types'] = [
        $entity_type_id,
      ];
      $this->derivatives[$entity_type_id]['label'] = t('@entity_type selection', [
        '@entity_type' => $entity_type
          ->getLabel(),
      ]);
      $this->derivatives[$entity_type_id]['base_plugin_label'] = (string) $base_plugin_definition['label'];

      // If the entity type doesn't provide a 'label' key in its plugin
      // definition, we have to use the alternate PhpSelection class as default
      // plugin, which allows filtering the target entities by their label()
      // method. The major downside of PhpSelection is that it is more expensive
      // performance-wise than DefaultSelection because it has to load all the
      // target entities in order to perform the filtering process, regardless
      // of whether a limit has been passed.
      // @see \Drupal\Core\Entity\Plugin\EntityReferenceSelection\PhpSelection
      if (!$entity_type
        ->hasKey('label')) {
        $this->derivatives[$entity_type_id]['class'] = 'Drupal\\Core\\Entity\\Plugin\\EntityReferenceSelection\\PhpSelection';
      }
    }
    return parent::getDerivativeDefinitions($base_plugin_definition);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DefaultSelectionDeriver::$deprecatedProperties protected property
DefaultSelectionDeriver::$entityTypeManager protected property The entity type manager.
DefaultSelectionDeriver::create public static function Creates a new class instance. Overrides ContainerDeriverInterface::create
DefaultSelectionDeriver::getDerivativeDefinitions public function Gets the definition of all derivatives of a base plugin. Overrides DeriverBase::getDerivativeDefinitions
DefaultSelectionDeriver::__construct public function Creates a DefaultSelectionDeriver object.
DeprecatedServicePropertyTrait::__get public function Allows to access deprecated/removed properties.
DeriverBase::$derivatives protected property List of derivative definitions. 1
DeriverBase::getDerivativeDefinition public function Gets the definition of a derivative plugin. Overrides DeriverInterface::getDerivativeDefinition