BlockPluginInterface.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. namespace Drupal\Core\Block;
  3. use Drupal\Component\Plugin\ConfigurablePluginInterface;
  4. use Drupal\Component\Plugin\DerivativeInspectionInterface;
  5. use Drupal\Core\Cache\CacheableDependencyInterface;
  6. use Drupal\Component\Plugin\PluginInspectionInterface;
  7. use Drupal\Component\Plugin\ConfigurableInterface;
  8. use Drupal\Component\Plugin\DependentPluginInterface;
  9. use Drupal\Core\Form\FormStateInterface;
  10. use Drupal\Core\Plugin\PluginFormInterface;
  11. use Drupal\Core\Session\AccountInterface;
  12. /**
  13. * Defines the required interface for all block plugins.
  14. *
  15. * @todo Add detailed documentation here explaining the block system's
  16. * architecture and the relationships between the various objects, including
  17. * brief references to the important components that are not coupled to the
  18. * interface.
  19. *
  20. * @ingroup block_api
  21. */
  22. interface BlockPluginInterface extends ConfigurableInterface, DependentPluginInterface, ConfigurablePluginInterface, PluginFormInterface, PluginInspectionInterface, CacheableDependencyInterface, DerivativeInspectionInterface {
  23. /**
  24. * Indicates the block label (title) should be displayed to end users.
  25. */
  26. const BLOCK_LABEL_VISIBLE = 'visible';
  27. /**
  28. * Returns the user-facing block label.
  29. *
  30. * @todo Provide other specific label-related methods in
  31. * https://www.drupal.org/node/2025649.
  32. *
  33. * @return string
  34. * The block label.
  35. */
  36. public function label();
  37. /**
  38. * Indicates whether the block should be shown.
  39. *
  40. * This method allows base implementations to add general access restrictions
  41. * that should apply to all extending block plugins.
  42. *
  43. * @param \Drupal\Core\Session\AccountInterface $account
  44. * The user session for which to check access.
  45. * @param bool $return_as_object
  46. * (optional) Defaults to FALSE.
  47. *
  48. * @return bool|\Drupal\Core\Access\AccessResultInterface
  49. * The access result. Returns a boolean if $return_as_object is FALSE (this
  50. * is the default) and otherwise an AccessResultInterface object.
  51. * When a boolean is returned, the result of AccessInterface::isAllowed() is
  52. * returned, i.e. TRUE means access is explicitly allowed, FALSE means
  53. * access is either explicitly forbidden or "no opinion".
  54. *
  55. * @see \Drupal\block\BlockAccessControlHandler
  56. */
  57. public function access(AccountInterface $account, $return_as_object = FALSE);
  58. /**
  59. * Builds and returns the renderable array for this block plugin.
  60. *
  61. * If a block should not be rendered because it has no content, then this
  62. * method must also ensure to return no content: it must then only return an
  63. * empty array, or an empty array with #cache set (with cacheability metadata
  64. * indicating the circumstances for it being empty).
  65. *
  66. * @return array
  67. * A renderable array representing the content of the block.
  68. *
  69. * @see \Drupal\block\BlockViewBuilder
  70. */
  71. public function build();
  72. /**
  73. * Sets a particular value in the block settings.
  74. *
  75. * @param string $key
  76. * The key of PluginBase::$configuration to set.
  77. * @param mixed $value
  78. * The value to set for the provided key.
  79. *
  80. * @todo This doesn't belong here. Move this into a new base class in
  81. * https://www.drupal.org/node/1764380.
  82. * @todo This does not set a value in \Drupal::config(), so the name is confusing.
  83. *
  84. * @see \Drupal\Component\Plugin\PluginBase::$configuration
  85. */
  86. public function setConfigurationValue($key, $value);
  87. /**
  88. * Returns the configuration form elements specific to this block plugin.
  89. *
  90. * Blocks that need to add form elements to the normal block configuration
  91. * form should implement this method.
  92. *
  93. * @param array $form
  94. * The form definition array for the block configuration form.
  95. * @param \Drupal\Core\Form\FormStateInterface $form_state
  96. * The current state of the form.
  97. *
  98. * @return array
  99. * The renderable form array representing the entire configuration form.
  100. */
  101. public function blockForm($form, FormStateInterface $form_state);
  102. /**
  103. * Adds block type-specific validation for the block form.
  104. *
  105. * Note that this method takes the form structure and form state for the full
  106. * block configuration form as arguments, not just the elements defined in
  107. * BlockPluginInterface::blockForm().
  108. *
  109. * @param array $form
  110. * The form definition array for the full block configuration form.
  111. * @param \Drupal\Core\Form\FormStateInterface $form_state
  112. * The current state of the form.
  113. *
  114. * @see \Drupal\Core\Block\BlockPluginInterface::blockForm()
  115. * @see \Drupal\Core\Block\BlockPluginInterface::blockSubmit()
  116. */
  117. public function blockValidate($form, FormStateInterface $form_state);
  118. /**
  119. * Adds block type-specific submission handling for the block form.
  120. *
  121. * Note that this method takes the form structure and form state for the full
  122. * block configuration form as arguments, not just the elements defined in
  123. * BlockPluginInterface::blockForm().
  124. *
  125. * @param array $form
  126. * The form definition array for the full block configuration form.
  127. * @param \Drupal\Core\Form\FormStateInterface $form_state
  128. * The current state of the form.
  129. *
  130. * @see \Drupal\Core\Block\BlockPluginInterface::blockForm()
  131. * @see \Drupal\Core\Block\BlockPluginInterface::blockValidate()
  132. */
  133. public function blockSubmit($form, FormStateInterface $form_state);
  134. /**
  135. * Suggests a machine name to identify an instance of this block.
  136. *
  137. * The block plugin need not verify that the machine name is at all unique. It
  138. * is only responsible for providing a baseline suggestion; calling code is
  139. * responsible for ensuring whatever uniqueness is required for the use case.
  140. *
  141. * @return string
  142. * The suggested machine name.
  143. */
  144. public function getMachineNameSuggestion();
  145. }