ContextAwarePluginManagerTrait.php 813 B

12345678910111213141516171819202122232425262728293031
  1. <?php
  2. namespace Drupal\Core\Plugin\Context;
  3. /**
  4. * Provides a trait for plugin managers that support context-aware plugins.
  5. */
  6. trait ContextAwarePluginManagerTrait {
  7. /**
  8. * Wraps the context handler.
  9. *
  10. * @return \Drupal\Core\Plugin\Context\ContextHandlerInterface
  11. */
  12. protected function contextHandler() {
  13. return \Drupal::service('context.handler');
  14. }
  15. /**
  16. * See \Drupal\Core\Plugin\Context\ContextAwarePluginManagerInterface::getDefinitionsForContexts().
  17. */
  18. public function getDefinitionsForContexts(array $contexts = []) {
  19. return $this->contextHandler()->filterPluginDefinitionsByContexts($contexts, $this->getDefinitions());
  20. }
  21. /**
  22. * See \Drupal\Component\Plugin\Discovery\DiscoveryInterface::getDefinitions().
  23. */
  24. abstract public function getDefinitions();
  25. }