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