You are here

class EntityMatcherDeriver in Linkit 8.5

Same name and namespace in other branches
  1. 8.4 src/Plugin/Derivative/EntityMatcherDeriver.php \Drupal\linkit\Plugin\Derivative\EntityMatcherDeriver

Provides matchers based on active entities.

Hierarchy

Expanded class hierarchy of EntityMatcherDeriver

See also

Plugin API

File

src/Plugin/Derivative/EntityMatcherDeriver.php, line 16

Namespace

Drupal\linkit\Plugin\Derivative
View source
class EntityMatcherDeriver extends DeriverBase implements ContainerDeriverInterface {

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

  /**
   * Creates an EntityMatcherDeriver 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) {
      $canonical = $entity_type
        ->getLinkTemplate('canonical');
      $edit_form = $entity_type
        ->getLinkTemplate('edit-form');

      // Only entities that has a distinct canonical URI that is not the same
      // as the edit-form URI will be derived.
      if ($entity_type instanceof ContentEntityTypeInterface && $canonical && $canonical !== $edit_form || $entity_type_id === 'media') {
        $this->derivatives[$entity_type_id] = $base_plugin_definition;
        $this->derivatives[$entity_type_id]['id'] = $base_plugin_definition['id'] . ':' . $entity_type_id;
        $this->derivatives[$entity_type_id]['label'] = $entity_type
          ->getLabel();
        $this->derivatives[$entity_type_id]['target_entity'] = $entity_type_id;
        $this->derivatives[$entity_type_id]['base_plugin_label'] = (string) $base_plugin_definition['label'];
      }
    }
    return parent::getDerivativeDefinitions($base_plugin_definition);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DeriverBase::$derivatives protected property List of derivative definitions. 1
DeriverBase::getDerivativeDefinition public function Gets the definition of a derivative plugin. Overrides DeriverInterface::getDerivativeDefinition
EntityMatcherDeriver::$entityTypeManager protected property The entity type manager.
EntityMatcherDeriver::create public static function Creates a new class instance. Overrides ContainerDeriverInterface::create
EntityMatcherDeriver::getDerivativeDefinitions public function Gets the definition of all derivatives of a base plugin. Overrides DeriverBase::getDerivativeDefinitions
EntityMatcherDeriver::__construct public function Creates an EntityMatcherDeriver object.