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