settings_tray.module 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. /**
  3. * @file
  4. * Allows configuring blocks and other configuration from the site front-end.
  5. */
  6. use Drupal\Core\Asset\AttachedAssetsInterface;
  7. use Drupal\Core\Routing\RouteMatchInterface;
  8. use Drupal\block\entity\Block;
  9. use Drupal\block\BlockInterface;
  10. use Drupal\settings_tray\Block\BlockEntitySettingTrayForm;
  11. /**
  12. * Implements hook_help().
  13. */
  14. function settings_tray_help($route_name, RouteMatchInterface $route_match) {
  15. switch ($route_name) {
  16. case 'help.page.settings_tray':
  17. $output = '<h3>' . t('About') . '</h3>';
  18. $output .= '<p>' . t('The Settings Tray module allows users with the <a href=":administer_block_permission">Administer blocks</a> and <a href=":contextual_permission">Use contextual links</a> permissions to edit blocks without visiting a separate page. For more information, see the <a href=":handbook_url">online documentation for the Settings Tray module</a>.', [':handbook_url' => 'https://www.drupal.org/documentation/modules/settings_tray', ':administer_block_permission' => \Drupal::url('user.admin_permissions', [], ['fragment' => 'module-block']), ':contextual_permission' => \Drupal::url('user.admin_permissions', [], ['fragment' => 'module-contextual'])]) . '</p>';
  19. $output .= '<h3>' . t('Uses') . '</h3>';
  20. $output .= '<dl>';
  21. $output .= '<dt>' . t('Editing blocks in place') . '</dt>';
  22. $output .= '<dd>';
  23. $output .= '<p>' . t('To edit blocks in place, either click the <strong>Edit</strong> button in the toolbar and then click on the block, or choose "Quick edit" from the block\'s contextual link. (See the <a href=":contextual">Contextual Links module help</a> for more information about how to use contextual links.)', [':contextual' => \Drupal::url('help.page', ['name' => 'contextual'])]) . '</p>';
  24. $output .= '<p>' . t('The Settings Tray for the block will open in a sidebar, with a compact form for configuring what the block shows.') . '</p>';
  25. $output .= '<p>' . t('Save the form and the changes will be immediately visible on the page.') . '</p>';
  26. $output .= '</dd>';
  27. $output .= '</dl>';
  28. return ['#markup' => $output];
  29. }
  30. }
  31. /**
  32. * Implements hook_contextual_links_view_alter().
  33. *
  34. * Change Configure Blocks into off_canvas links.
  35. */
  36. function settings_tray_contextual_links_view_alter(&$element, $items) {
  37. if (isset($element['#links']['settings-trayblock-configure'])) {
  38. // Place settings_tray link first.
  39. $settings_tray_link = $element['#links']['settings-trayblock-configure'];
  40. unset($element['#links']['settings-trayblock-configure']);
  41. $element['#links'] = ['settings-trayblock-configure' => $settings_tray_link] + $element['#links'];
  42. // If this is content block change title to avoid duplicate "Quick Edit".
  43. if (isset($element['#links']['block-contentblock-edit'])) {
  44. $element['#links']['settings-trayblock-configure']['title'] = t('Quick edit settings');
  45. }
  46. $element['#attached']['library'][] = 'core/drupal.dialog.off_canvas';
  47. }
  48. }
  49. /**
  50. * Checks if a block has overrides.
  51. *
  52. * @param \Drupal\block\BlockInterface $block
  53. * The block to check for overrides.
  54. *
  55. * @return bool
  56. * TRUE if the block has overrides otherwise FALSE.
  57. *
  58. * @internal
  59. */
  60. function _settings_tray_has_block_overrides(BlockInterface $block) {
  61. // @todo Replace the following with $block->hasOverrides() in https://www.drupal.org/project/drupal/issues/2910353
  62. // and remove this function.
  63. return \Drupal::config($block->getEntityType()->getConfigPrefix() . '.' . $block->id())->hasOverrides();
  64. }
  65. /**
  66. * Implements hook_block_view_alter().
  67. */
  68. function settings_tray_block_view_alter(array &$build) {
  69. if (isset($build['#contextual_links']['block'])) {
  70. // Ensure that contextual links vary by whether the block has config overrides
  71. // or not.
  72. // @see _contextual_links_to_id()
  73. $build['#contextual_links']['block']['metadata']['has_overrides'] = _settings_tray_has_block_overrides($build['#block']) ? 1 : 0;
  74. }
  75. // Force a new 'data-contextual-id' attribute on blocks when this module is
  76. // enabled so as not to reuse stale data cached client-side.
  77. // @todo Remove when https://www.drupal.org/node/2773591 is fixed.
  78. $build['#contextual_links']['settings_tray'] = [
  79. 'route_parameters' => [],
  80. ];
  81. }
  82. /**
  83. * Implements hook_entity_type_build().
  84. */
  85. function settings_tray_entity_type_build(array &$entity_types) {
  86. /* @var $entity_types \Drupal\Core\Entity\EntityTypeInterface[] */
  87. $entity_types['block']
  88. ->setFormClass('settings_tray', BlockEntitySettingTrayForm::class)
  89. ->setLinkTemplate('settings_tray-form', '/admin/structure/block/manage/{block}/settings-tray');
  90. }
  91. /**
  92. * Implements hook_preprocess_HOOK() for block templates.
  93. */
  94. function settings_tray_preprocess_block(&$variables) {
  95. // Only blocks that have a settings_tray form and have no configuration
  96. // overrides will have a "Quick Edit" link. We could wait for the contextual
  97. // links to be initialized on the client side, and then add the class and
  98. // data- attribute below there (via JavaScript). But that would mean that it
  99. // would be impossible to show Settings Tray's clickable regions immediately
  100. // when the page loads. When latency is high, this will cause flicker.
  101. // @see \Drupal\settings_tray\Access\BlockPluginHasSettingsTrayFormAccessCheck
  102. /** @var \Drupal\settings_tray\Access\BlockPluginHasSettingsTrayFormAccessCheck $access_checker */
  103. $access_checker = \Drupal::service('access_check.settings_tray.block.settings_tray_form');
  104. /** @var \Drupal\Core\Block\BlockManagerInterface $block_plugin_manager */
  105. $block_plugin_manager = \Drupal::service('plugin.manager.block');
  106. /** @var \Drupal\Core\Block\BlockPluginInterface $block_plugin */
  107. $block_plugin = $block_plugin_manager->createInstance($variables['plugin_id']);
  108. if (isset($variables['elements']['#contextual_links']['block']['route_parameters']['block'])) {
  109. $block = Block::load($variables['elements']['#contextual_links']['block']['route_parameters']['block']);
  110. if ($access_checker->accessBlockPlugin($block_plugin)->isAllowed() && !_settings_tray_has_block_overrides($block)) {
  111. // Add class and attributes to all blocks to allow Javascript to target.
  112. $variables['attributes']['class'][] = 'settings-tray-editable';
  113. $variables['attributes']['data-drupal-settingstray'] = 'editable';
  114. }
  115. }
  116. }
  117. /**
  118. * Implements hook_toolbar_alter().
  119. *
  120. * Alters the 'contextual' toolbar tab if it exists (meaning the user is allowed
  121. * to use contextual links) and if they can administer blocks.
  122. *
  123. * @todo Remove the "administer blocks" requirement in
  124. * https://www.drupal.org/node/2822965.
  125. *
  126. * @see contextual_toolbar()
  127. */
  128. function settings_tray_toolbar_alter(&$items) {
  129. $items['contextual']['#cache']['contexts'][] = 'user.permissions';
  130. if (isset($items['contextual']['tab']) && \Drupal::currentUser()->hasPermission('administer blocks')) {
  131. $items['contextual']['#weight'] = -1000;
  132. $items['contextual']['#attached']['library'][] = 'settings_tray/drupal.settings_tray';
  133. $items['contextual']['tab']['#attributes']['data-drupal-settingstray'] = 'toggle';
  134. // Set a class on items to mark whether they should be active in edit mode.
  135. // @todo Create a dynamic method for modules to set their own items.
  136. // https://www.drupal.org/node/2784589.
  137. $edit_mode_items = ['contextual', 'block_place'];
  138. foreach ($items as $key => $item) {
  139. if (!in_array($key, $edit_mode_items) && (!isset($items[$key]['#wrapper_attributes']['class']) || !in_array('hidden', $items[$key]['#wrapper_attributes']['class']))) {
  140. $items[$key]['#wrapper_attributes']['class'][] = 'edit-mode-inactive';
  141. }
  142. }
  143. }
  144. }
  145. /**
  146. * Implements hook_block_alter().
  147. *
  148. * Ensures every block plugin definition has an 'settings_tray' form specified.
  149. *
  150. * @see \Drupal\settings_tray\Access\BlockPluginHasSettingsTrayFormAccessCheck
  151. */
  152. function settings_tray_block_alter(&$definitions) {
  153. foreach ($definitions as &$definition) {
  154. // If a block plugin does not define its own 'settings_tray' form, use the
  155. // plugin class itself.
  156. if (!isset($definition['forms']['settings_tray'])) {
  157. $definition['forms']['settings_tray'] = $definition['class'];
  158. }
  159. }
  160. }
  161. /**
  162. * Implements hook_css_alter().
  163. */
  164. function settings_tray_css_alter(&$css, AttachedAssetsInterface $assets) {
  165. // @todo Remove once conditional ordering is introduced in
  166. // https://www.drupal.org/node/1945262.
  167. $path = drupal_get_path('module', 'settings_tray') . '/css/settings_tray.theme.css';
  168. if (isset($css[$path])) {
  169. // Use 200 to come after CSS_AGGREGATE_THEME.
  170. $css[$path]['group'] = 200;
  171. }
  172. }