ContextProviderInterface.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace Drupal\Core\Plugin\Context;
  3. /**
  4. * Defines an interface for providing plugin contexts.
  5. *
  6. * Implementations only need to deal with unqualified context IDs so they only
  7. * need to be unique in the context of a given service provider.
  8. *
  9. * The fully qualified context ID then includes the service ID:
  10. * @{service_id}:{unqualified_context_id}.
  11. *
  12. * @see \Drupal\Core\Plugin\Context\ContextRepositoryInterface
  13. */
  14. interface ContextProviderInterface {
  15. /**
  16. * Gets runtime context values for the given context IDs.
  17. *
  18. * For context-aware plugins to function correctly, all of the contexts that
  19. * they require must be populated with values. So this method should set a
  20. * value for each context that it adds. For example:
  21. *
  22. * @code
  23. * // Determine a specific node to pass as context to a block.
  24. * $node = ...
  25. *
  26. * // Set that specific node as the value of the 'node' context.
  27. * $context = new Context(new ContextDefinition('entity:node'), $node);
  28. * return ['node' => $context];
  29. * @endcode
  30. *
  31. * On the other hand, there are cases, on which providers no longer are
  32. * possible to provide context objects, even without the value, so the caller
  33. * should not expect it.
  34. *
  35. * @param string[] $unqualified_context_ids
  36. * The requested context IDs. The context provider must only return contexts
  37. * for those IDs.
  38. *
  39. * @return \Drupal\Core\Plugin\Context\ContextInterface[]
  40. * The determined available contexts, keyed by the unqualified context_id.
  41. *
  42. * @see \Drupal\Core\Plugin\Context\ContextProviderInterface:getAvailableContexts()
  43. */
  44. public function getRuntimeContexts(array $unqualified_context_ids);
  45. /**
  46. * Gets all available contexts for the purposes of configuration.
  47. *
  48. * When a context aware plugin is being configured, the configuration UI must
  49. * know which named contexts are potentially available, but does not care
  50. * about the value, since the value can be different for each request, and
  51. * might not be available at all during the configuration UI's request.
  52. *
  53. * For example:
  54. * @code
  55. * // During configuration, there is no specific node to pass as context.
  56. * // However, inform the system that a context named 'node' is
  57. * // available, and provide its definition, so that context aware plugins
  58. * // can be configured to use it. When the plugin, for example a block,
  59. * // needs to evaluate the context, the value of this context will be
  60. * // supplied by getRuntimeContexts().
  61. * $context = new Context(new ContextDefinition('entity:node'));
  62. * return ['node' => $context];
  63. * @endcode
  64. *
  65. * @return \Drupal\Core\Plugin\Context\ContextInterface[]
  66. * All available contexts keyed by the unqualified context ID.
  67. *
  68. * @see \Drupal\Core\Plugin\Context\ContextProviderInterface::getRuntimeContext()
  69. */
  70. public function getAvailableContexts();
  71. }