outside_in.module 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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\outside_in\Block\BlockEntityOffCanvasForm;
  9. use Drupal\outside_in\Form\SystemBrandingOffCanvasForm;
  10. use Drupal\outside_in\Form\SystemMenuOffCanvasForm;
  11. /**
  12. * Implements hook_help().
  13. */
  14. function outside_in_help($route_name, RouteMatchInterface $route_match) {
  15. switch ($route_name) {
  16. case 'help.page.outside_in':
  17. $output = '<h3>' . t('About') . '</h3>';
  18. $output .= '<p>' . t('The Settings Tray module provides an \'edit mode\' in which clicking on a block opens a slide-out tray which allows configuration to be altered without leaving the page.For more information, see the <a href=":outside-in-documentation">online documentation for the Settings Tray module</a>.', [':outside-in-documentation' => 'https://www.drupal.org/documentation/modules/outside_in']) . '</p>';
  19. $output .= '<h3>' . t('Uses') . '</h3>';
  20. $output .= '<dl>';
  21. $output .= '<dt>' . t('Editing blocks on the same page in the slide-out tray') . '</dt>';
  22. $output .= '</dl>';
  23. return ['#markup' => $output];
  24. }
  25. }
  26. /**
  27. * Implements hook_contextual_links_view_alter().
  28. *
  29. * Change Configure Blocks into off_canvas links.
  30. */
  31. function outside_in_contextual_links_view_alter(&$element, $items) {
  32. if (isset($element['#links']['outside-inblock-configure'])) {
  33. // Place outside_in link first.
  34. $outside_in_link = $element['#links']['outside-inblock-configure'];
  35. unset($element['#links']['outside-inblock-configure']);
  36. $element['#links'] = ['outside-inblock-configure' => $outside_in_link] + $element['#links'];
  37. $element['#links']['outside-inblock-configure']['attributes'] = [
  38. 'class' => ['use-ajax'],
  39. 'data-dialog-type' => 'dialog',
  40. 'data-dialog-renderer' => 'off_canvas',
  41. 'data-outside-in-edit' => TRUE,
  42. ];
  43. // If this is content block change title to avoid duplicate "Quick Edit".
  44. if (isset($element['#links']['block-contentblock-edit'])) {
  45. $element['#links']['outside-inblock-configure']['title'] = t('Quick edit settings');
  46. }
  47. $element['#attached']['library'][] = 'outside_in/drupal.off_canvas';
  48. }
  49. }
  50. /**
  51. * Implements hook_block_view_alter().
  52. */
  53. function outside_in_block_view_alter(array &$build) {
  54. // Force a new 'data-contextual-id' attribute on blocks when this module is
  55. // enabled so as not to reuse stale data cached client-side.
  56. // @todo Remove when https://www.drupal.org/node/2773591 is fixed.
  57. $build['#contextual_links']['outside_in'] = [
  58. 'route_parameters' => [],
  59. ];
  60. }
  61. /**
  62. * Implements hook_element_info_alter().
  63. */
  64. function outside_in_element_info_alter(&$type) {
  65. if (isset($type['page'])) {
  66. $type['page']['#theme_wrappers']['outside_in_page_wrapper'] = ['#weight' => -1000];
  67. }
  68. }
  69. /**
  70. * Implements hook_theme().
  71. */
  72. function outside_in_theme() {
  73. return [
  74. 'outside_in_page_wrapper' => [
  75. 'variables' => ['children' => NULL],
  76. ],
  77. ];
  78. }
  79. /**
  80. * Implements hook_entity_type_build().
  81. */
  82. function outside_in_entity_type_build(array &$entity_types) {
  83. /* @var $entity_types \Drupal\Core\Entity\EntityTypeInterface[] */
  84. $entity_types['block']
  85. ->setFormClass('off_canvas', BlockEntityOffCanvasForm::class)
  86. ->setLinkTemplate('off_canvas-form', '/admin/structure/block/manage/{block}/off-canvas');
  87. }
  88. /**
  89. * Implements hook_preprocess_HOOK() for block templates.
  90. */
  91. function outside_in_preprocess_block(&$variables) {
  92. // The main system block does not contain the block contextual links.
  93. $variables['#cache']['contexts'][] = 'outside_in_is_applied';
  94. if ($variables['plugin_id'] !== 'system_main_block' && \Drupal::service('outside_in.manager')->isApplicable()) {
  95. // Add class and attributes to all blocks to allow Javascript to target.
  96. $variables['attributes']['class'][] = 'outside-in-editable';
  97. $variables['attributes']['data-drupal-outsidein'] = 'editable';
  98. }
  99. }
  100. /**
  101. * Implements hook_toolbar_alter().
  102. *
  103. * Includes outside_library if Edit link is in toolbar.
  104. */
  105. function outside_in_toolbar_alter(&$items) {
  106. $items['contextual']['#cache']['contexts'][] = 'outside_in_is_applied';
  107. if (isset($items['contextual']['tab']) && \Drupal::service('outside_in.manager')->isApplicable()) {
  108. $items['contextual']['#weight'] = -1000;
  109. $items['contextual']['#attached']['library'][] = 'outside_in/drupal.outside_in';
  110. $items['contextual']['tab']['#attributes']['data-drupal-outsidein'] = 'toggle';
  111. // Set a class on items to mark whether they should be active in edit mode.
  112. // @todo Create a dynamic method for modules to set their own items.
  113. // https://www.drupal.org/node/2784589
  114. $edit_mode_items = ['contextual', 'block_place'];
  115. foreach ($items as $key => $item) {
  116. if (!in_array($key, $edit_mode_items) && (!isset($items[$key]['#wrapper_attributes']['class']) || !in_array('hidden', $items[$key]['#wrapper_attributes']['class']))) {
  117. $items[$key]['#wrapper_attributes']['class'][] = 'edit-mode-inactive';
  118. }
  119. }
  120. }
  121. }
  122. /**
  123. * Implements hook_block_alter().
  124. */
  125. function outside_in_block_alter(&$definitions) {
  126. if (!empty($definitions['system_branding_block'])) {
  127. $definitions['system_branding_block']['forms']['off_canvas'] = SystemBrandingOffCanvasForm::class;
  128. }
  129. // Since menu blocks use derivatives, check the definition ID instead of
  130. // relying on the plugin ID.
  131. foreach ($definitions as &$definition) {
  132. if ($definition['id'] === 'system_menu_block') {
  133. $definition['forms']['off_canvas'] = SystemMenuOffCanvasForm::class;
  134. }
  135. }
  136. }
  137. /**
  138. * Implements hook_css_alter().
  139. */
  140. function outside_in_css_alter(&$css, AttachedAssetsInterface $assets) {
  141. // @todo Remove once conditional ordering is introduced in
  142. // https://www.drupal.org/node/1945262.
  143. $path = drupal_get_path('module', 'outside_in') . '/css/outside_in.theme.css';
  144. if (isset($css[$path])) {
  145. // Use 200 to come after CSS_AGGREGATE_THEME.
  146. $css[$path]['group'] = 200;
  147. }
  148. }