ContextHandlerInterface.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. namespace Drupal\Core\Plugin\Context;
  3. use Drupal\Core\Plugin\ContextAwarePluginInterface;
  4. /**
  5. * Provides an interface for handling sets of contexts.
  6. */
  7. interface ContextHandlerInterface {
  8. /**
  9. * Determines plugins whose constraints are satisfied by a set of contexts.
  10. *
  11. * @todo Use context definition objects after
  12. * https://www.drupal.org/node/2281635.
  13. *
  14. * @param \Drupal\Component\Plugin\Context\ContextInterface[] $contexts
  15. * An array of contexts.
  16. * @param array $definitions
  17. * An array of plugin definitions.
  18. *
  19. * @return array
  20. * An array of plugin definitions.
  21. */
  22. public function filterPluginDefinitionsByContexts(array $contexts, array $definitions);
  23. /**
  24. * Checks a set of requirements against a set of contexts.
  25. *
  26. * @todo Use context definition objects after
  27. * https://www.drupal.org/node/2281635.
  28. *
  29. * @param \Drupal\Component\Plugin\Context\ContextInterface[] $contexts
  30. * An array of available contexts.
  31. * @param \Drupal\Core\TypedData\DataDefinitionInterface[] $requirements
  32. * An array of requirements.
  33. *
  34. * @return bool
  35. * TRUE if all of the requirements are satisfied by the context, FALSE
  36. * otherwise.
  37. */
  38. public function checkRequirements(array $contexts, array $requirements);
  39. /**
  40. * Determines which contexts satisfy the constraints of a given definition.
  41. *
  42. * @todo Use context definition objects after
  43. * https://www.drupal.org/node/2281635.
  44. *
  45. * @param \Drupal\Component\Plugin\Context\ContextInterface[] $contexts
  46. * An array of contexts.
  47. * @param \Drupal\Core\Plugin\Context\ContextDefinitionInterface $definition
  48. * The definition to satisfy.
  49. *
  50. * @return \Drupal\Component\Plugin\Context\ContextInterface[]
  51. * An array of matching contexts.
  52. */
  53. public function getMatchingContexts(array $contexts, ContextDefinitionInterface $definition);
  54. /**
  55. * Prepares a plugin for evaluation.
  56. *
  57. * @param \Drupal\Core\Plugin\ContextAwarePluginInterface $plugin
  58. * A plugin about to be evaluated.
  59. * @param \Drupal\Core\Plugin\Context\ContextInterface[] $contexts
  60. * An array of contexts to set on the plugin. They will only be set if they
  61. * match the plugin's context definitions.
  62. * @param array $mappings
  63. * (optional) A mapping of the expected assignment names to their context
  64. * names. For example, if one of the $contexts is named 'current_user', but the
  65. * plugin expects a context named 'user', then this map would contain
  66. * 'user' => 'current_user'.
  67. *
  68. * @throws \Drupal\Component\Plugin\Exception\ContextException
  69. * Thrown when a context assignment was not satisfied.
  70. * @throws \Drupal\Component\Plugin\Exception\MissingValueContextException
  71. * Thrown when a context is provided but has no value. Only thrown if
  72. * no contexts are missing.
  73. */
  74. public function applyContextMapping(ContextAwarePluginInterface $plugin, $contexts, $mappings = []);
  75. }