BlockPluginInterface.php 5.6 KB

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