3 namespace Drupal\entity_embed\EntityEmbedDisplay;
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;
12 * Provides an Entity Embed display plugin manager.
14 * @see \Drupal\entity_embed\Annotation\EntityEmbedDisplay
15 * @see \Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayBase
16 * @see \Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayInterface
19 class EntityEmbedDisplayManager extends DefaultPluginManager {
22 * Constructs a new EntityEmbedDisplayManager.
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.
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');
39 * Overrides DefaultPluginManager::processDefinition().
41 public function processDefinition(&$definition, $plugin_id) {
43 'entity_types' => FALSE,
46 if ($definition['entity_types'] !== FALSE && !is_array($definition['entity_types'])) {
47 $definition['entity_types'] = array($definition['entity_types']);
52 * Determines plugins whose constraints are satisfied by a set of contexts.
54 * @param array $contexts
55 * An array of contexts.
58 * An array of plugin definitions.
60 * @todo At some point convert this to use ContextAwarePluginManagerTrait
62 * @see https://drupal.org/node/2277981
64 public function getDefinitionsForContexts(array $contexts = array()) {
65 $definitions = $this->getDefinitions();
66 $valid_ids = array_filter(array_keys($definitions), function ($id) use ($contexts) {
68 $display = $this->createInstance($id);
69 foreach ($contexts as $name => $value) {
70 $display->setContextValue($name, $value);
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();
76 catch (PluginException $e) {
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;
86 * Gets definition options for entity.
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.
91 * @param \Drupal\Core\Entity\EntityInterface $entity
95 * An array of valid plugin labels, keyed by plugin ID.
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'];
106 * Filters out plugins from definitions that should be hidden in the UI.
108 * @param array $definitions
109 * The array of plugin definitions.
112 * Returns plugin definitions that should be displayed in the UI.
114 protected function filterExposedDefinitions(array $definitions) {
115 return array_filter($definitions, function($definition) {
116 return empty($definition['no_ui']);
121 * Gets definition options for entity type.
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.
126 * @param string $entity_type
127 * The entity type id.
130 * An array of valid plugin labels, keyed by plugin ID.
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'];