Interim commit.
[yaffs-website] / web / modules / contrib / entity_embed / src / EntityEmbedDisplay / EntityEmbedDisplayManager.php
1 <?php
2
3 namespace Drupal\entity_embed\EntityEmbedDisplay;
4
5 use Drupal\Core\Cache\CacheBackendInterface;
6 use Drupal\Core\Entity\EntityInterface;
7 use Drupal\Core\Extension\ModuleHandlerInterface;
8 use Drupal\Core\Plugin\DefaultPluginManager;
9 use Drupal\Component\Plugin\Exception\PluginException;
10
11 /**
12  * Provides an Entity Embed display plugin manager.
13  *
14  * @see \Drupal\entity_embed\Annotation\EntityEmbedDisplay
15  * @see \Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayBase
16  * @see \Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayInterface
17  * @see plugin_api
18  */
19 class EntityEmbedDisplayManager extends DefaultPluginManager {
20
21   /**
22    * Constructs a new EntityEmbedDisplayManager.
23    *
24    * @param \Traversable $namespaces
25    *   An object that implements \Traversable which contains the root paths
26    *   keyed by the corresponding namespace to look for plugin implementations.
27    * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
28    *   Cache backend instance to use.
29    * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
30    *   The module handler to invoke the alter hook with.
31    */
32   public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
33     parent::__construct('Plugin/entity_embed/EntityEmbedDisplay', $namespaces, $module_handler, 'Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayInterface', 'Drupal\entity_embed\Annotation\EntityEmbedDisplay');
34     $this->alterInfo('entity_embed_display_plugins');
35     $this->setCacheBackend($cache_backend, 'entity_embed_display_plugins');
36   }
37
38   /**
39    * Overrides DefaultPluginManager::processDefinition().
40    */
41   public function processDefinition(&$definition, $plugin_id) {
42     $definition += array(
43       'entity_types' => FALSE,
44     );
45
46     if ($definition['entity_types'] !== FALSE && !is_array($definition['entity_types'])) {
47       $definition['entity_types'] = array($definition['entity_types']);
48     }
49   }
50
51   /**
52    * Determines plugins whose constraints are satisfied by a set of contexts.
53    *
54    * @param array $contexts
55    *   An array of contexts.
56    *
57    * @return array
58    *   An array of plugin definitions.
59    *
60    * @todo At some point convert this to use ContextAwarePluginManagerTrait
61    *
62    * @see https://drupal.org/node/2277981
63    */
64   public function getDefinitionsForContexts(array $contexts = array()) {
65     $definitions = $this->getDefinitions();
66     $valid_ids = array_filter(array_keys($definitions), function ($id) use ($contexts) {
67       try {
68         $display = $this->createInstance($id);
69         foreach ($contexts as $name => $value) {
70           $display->setContextValue($name, $value);
71         }
72         // We lose cacheability metadata at this point. We should refactor to
73         // avoid this. @see https://www.drupal.org/node/2593379#comment-11368447
74         return $display->access()->isAllowed();
75       }
76       catch (PluginException $e) {
77         return FALSE;
78       }
79     });
80     $definitions_for_context = array_intersect_key($definitions, array_flip($valid_ids));
81     $this->moduleHandler->alter('entity_embed_display_plugins_for_context', $definitions_for_context, $contexts);
82     return $definitions_for_context;
83   }
84
85   /**
86    * Gets definition options for entity.
87    *
88    * Provides a list of plugins that can be used for a certain entity and
89    * filters out plugins that should be hidden in the UI.
90    *
91    * @param \Drupal\Core\Entity\EntityInterface $entity
92    *   An entity object.
93    *
94    * @return array
95    *   An array of valid plugin labels, keyed by plugin ID.
96    */
97   public function getDefinitionOptionsForEntity(EntityInterface $entity) {
98     $definitions = $this->getDefinitionsForContexts(array('entity' => $entity, 'entity_type' => $entity->getEntityTypeId()));
99     $definitions = $this->filterExposedDefinitions($definitions);
100     return array_map(function ($definition) {
101       return (string) $definition['label'];
102     }, $definitions);
103   }
104
105   /**
106    * Filters out plugins from definitions that should be hidden in the UI.
107    *
108    * @param array $definitions
109    *   The array of plugin definitions.
110    *
111    * @return array
112    *   Returns plugin definitions that should be displayed in the UI.
113    */
114   protected function filterExposedDefinitions(array $definitions) {
115     return array_filter($definitions, function($definition) {
116       return empty($definition['no_ui']);
117     });
118   }
119
120   /**
121    * Gets definition options for entity type.
122    *
123    * Provides a list of plugins that can be used for a certain entity type and
124    * filters out plugins that should be hidden in the UI.
125    *
126    * @param string $entity_type
127    *   The entity type id.
128    *
129    * @return array
130    *   An array of valid plugin labels, keyed by plugin ID.
131    */
132   public function getDefinitionOptionsForEntityType($entity_type) {
133     $definitions = $this->getDefinitionsForContexts(array('entity_type' => $entity_type));
134     $definitions = $this->filterExposedDefinitions($definitions);
135     return array_map(function ($definition) {
136       return (string) $definition['label'];
137     }, $definitions);
138   }
139
140 }