Yaffs site version 1.1
[yaffs-website] / web / modules / contrib / linkit / src / Plugin / Derivative / EntityMatcherDeriver.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\linkit\Plugin\Derivative\EntityMatcherDeriver.
6  */
7
8 namespace Drupal\linkit\Plugin\Derivative;
9
10 use Drupal\Component\Plugin\Derivative\DeriverBase;
11 use Drupal\Component\Utility\Unicode;
12 use Drupal\Core\Entity\EntityManagerInterface;
13 use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
14 use Symfony\Component\DependencyInjection\ContainerInterface;
15
16 /**
17  *
18  * @see plugin_api
19  */
20 class EntityMatcherDeriver extends DeriverBase implements ContainerDeriverInterface {
21
22   /**
23    * The entity manager.
24    *
25    * @var \Drupal\Core\Entity\EntityManagerInterface
26    */
27   protected $entityManager;
28
29   /**
30    * Creates an EntityMatcherDeriver object.
31    *
32    * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
33    *   The entity manager.
34    */
35   public function __construct(EntityManagerInterface $entity_manager) {
36     $this->entityManager = $entity_manager;
37   }
38
39   /**
40    * {@inheritdoc}
41    */
42   public static function create(ContainerInterface $container, $base_plugin_id) {
43     return new static(
44       $container->get('entity.manager')
45     );
46   }
47
48   /**
49    * {@inheritdoc}
50    */
51   public function getDerivativeDefinitions($base_plugin_definition) {
52     foreach ($this->entityManager->getDefinitions() as $entity_type_id => $entity_type) {
53       $has_canonical = $entity_type->hasLinkTemplate('canonical');
54
55       if ($has_canonical) {
56         $this->derivatives[$entity_type_id] = $base_plugin_definition;
57         $this->derivatives[$entity_type_id]['id'] = $base_plugin_definition['id'] . ':' . $entity_type_id;
58         $this->derivatives[$entity_type_id]['label'] = $entity_type->getLabel();
59         $this->derivatives[$entity_type_id]['target_entity'] = $entity_type_id;
60         $this->derivatives[$entity_type_id]['base_plugin_label'] = (string) $base_plugin_definition['label'];
61       }
62     }
63
64     return parent::getDerivativeDefinitions($base_plugin_definition);
65   }
66
67 }