You are here

public function EntityEmbedDisplayManager::getDefinitionsForContexts in Entity Embed 8

Determines plugins whose constraints are satisfied by a set of contexts.

@todo At some point convert this to use ContextAwarePluginManagerTrait

Parameters

array $contexts: An array of contexts.

Return value

array An array of plugin definitions.

See also

https://drupal.org/node/2277981

3 calls to EntityEmbedDisplayManager::getDefinitionsForContexts()
EntityEmbedDisplayManager::getDefinitionOptionsForContext in src/EntityEmbedDisplay/EntityEmbedDisplayManager.php
Gets definition options for context.
EntityEmbedDisplayManager::getDefinitionOptionsForEntity in src/EntityEmbedDisplay/EntityEmbedDisplayManager.php
Gets definition options for entity.
EntityEmbedDisplayManager::getDefinitionOptionsForEntityType in src/EntityEmbedDisplay/EntityEmbedDisplayManager.php
Gets definition options for entity type.

File

src/EntityEmbedDisplay/EntityEmbedDisplayManager.php, line 53

Class

EntityEmbedDisplayManager
Provides an Entity Embed display plugin manager.

Namespace

Drupal\entity_embed\EntityEmbedDisplay

Code

public function getDefinitionsForContexts(array $contexts = []) {
  $definitions = $this
    ->getDefinitions();
  if (!empty($contexts['embed_button'])) {
    $button_plugins = $contexts['embed_button']
      ->getTypeSetting('display_plugins');
    if (!empty($button_plugins)) {
      $allowed_definitions = [];
      foreach ($button_plugins as $plugin_id) {
        if (!empty($definitions[$plugin_id])) {
          $allowed_definitions[$plugin_id] = $definitions[$plugin_id];
        }
      }
      $definitions = $allowed_definitions;
    }
  }
  $valid_ids = array_filter(array_keys($definitions), function ($id) use ($contexts) {
    try {
      $display = $this
        ->createInstance($id);
      foreach ($contexts as $name => $value) {
        $display
          ->setContextValue($name, $value);
      }

      // We lose cacheability metadata at this point. We should refactor to
      // avoid this. @see https://www.drupal.org/node/2593379#comment-11368447
      return $display
        ->access()
        ->isAllowed();
    } catch (PluginException $e) {
      return FALSE;
    }
  });
  $definitions_for_context = array_intersect_key($definitions, array_flip($valid_ids));
  $this->moduleHandler
    ->alter('entity_embed_display_plugins_for_context', $definitions_for_context, $contexts);
  return $definitions_for_context;
}