ContextAwarePluginInterface.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. namespace Drupal\Component\Plugin;
  3. use Drupal\Component\Plugin\Context\ContextInterface;
  4. /**
  5. * Interface for defining context aware plugins.
  6. *
  7. * Context aware plugins can specify an array of context definitions keyed by
  8. * context name at the plugin definition under the "context" key.
  9. *
  10. * @ingroup plugin_api
  11. */
  12. interface ContextAwarePluginInterface extends PluginInspectionInterface {
  13. /**
  14. * Gets the context definitions of the plugin.
  15. *
  16. * @return \Drupal\Component\Plugin\Context\ContextDefinitionInterface[]
  17. * The array of context definitions, keyed by context name.
  18. */
  19. public function getContextDefinitions();
  20. /**
  21. * Gets a specific context definition of the plugin.
  22. *
  23. * @param string $name
  24. * The name of the context in the plugin definition.
  25. *
  26. * @return \Drupal\Component\Plugin\Context\ContextDefinitionInterface
  27. * The definition against which the context value must validate.
  28. *
  29. * @throws \Drupal\Component\Plugin\Exception\PluginException
  30. * If the requested context is not defined.
  31. */
  32. public function getContextDefinition($name);
  33. /**
  34. * Gets the defined contexts.
  35. *
  36. * @return array
  37. * The set context objects.
  38. *
  39. * @throws \Drupal\Component\Plugin\Exception\PluginException
  40. * If contexts are defined but not set.
  41. */
  42. public function getContexts();
  43. /**
  44. * Gets a defined context.
  45. *
  46. * @param string $name
  47. * The name of the context in the plugin definition.
  48. *
  49. * @return \Drupal\Component\Plugin\Context\ContextInterface
  50. * The context object.
  51. *
  52. * @throws \Drupal\Component\Plugin\Exception\PluginException
  53. * If the requested context is not set.
  54. */
  55. public function getContext($name);
  56. /**
  57. * Gets the values for all defined contexts.
  58. *
  59. * @return array
  60. * An array of set context values, keyed by context name. If a context is
  61. * unset its value is returned as NULL.
  62. */
  63. public function getContextValues();
  64. /**
  65. * Gets the value for a defined context.
  66. *
  67. * @param string $name
  68. * The name of the context in the plugin configuration.
  69. *
  70. * @return mixed
  71. * The currently set context value.
  72. *
  73. * @throws \Drupal\Component\Plugin\Exception\PluginException
  74. * If the requested context is not set.
  75. */
  76. public function getContextValue($name);
  77. /**
  78. * Set a context on this plugin.
  79. *
  80. * @param string $name
  81. * The name of the context in the plugin configuration.
  82. * @param \Drupal\Component\Plugin\Context\ContextInterface $context
  83. * The context object to set.
  84. */
  85. public function setContext($name, ContextInterface $context);
  86. /**
  87. * Sets the value for a defined context.
  88. *
  89. * @param string $name
  90. * The name of the context in the plugin definition.
  91. * @param mixed $value
  92. * The value to set the context to. The value has to validate against the
  93. * provided context definition.
  94. *
  95. * @return \Drupal\Component\Plugin\ContextAwarePluginInterface
  96. * A context aware plugin object for chaining.
  97. *
  98. * @throws \Drupal\Component\Plugin\Exception\PluginException
  99. * If the value does not pass validation.
  100. */
  101. public function setContextValue($name, $value);
  102. /**
  103. * Validates the set values for the defined contexts.
  104. *
  105. * @return \Symfony\Component\Validator\ConstraintViolationListInterface
  106. * A list of constraint violations. If the list is empty, validation
  107. * succeeded.
  108. */
  109. public function validateContexts();
  110. /**
  111. * Gets a mapping of the expected assignment names to their context names.
  112. *
  113. * @return array
  114. * A mapping of the expected assignment names to their context names. For
  115. * example, if one of the $contexts is named 'user.current_user', but the
  116. * plugin expects a context named 'user', then this map would contain
  117. * 'user' => 'user.current_user'.
  118. */
  119. public function getContextMapping();
  120. /**
  121. * Sets a mapping of the expected assignment names to their context names.
  122. *
  123. * @param array $context_mapping
  124. * A mapping of the expected assignment names to their context names. For
  125. * example, if one of the $contexts is named 'user.current_user', but the
  126. * plugin expects a context named 'user', then this map would contain
  127. * 'user' => 'user.current_user'.
  128. *
  129. * @return $this
  130. */
  131. public function setContextMapping(array $context_mapping);
  132. }