Chris@0: ' . t('About') . ''; Chris@18: $output .= '

' . t('The Settings Tray module allows users with the Administer blocks and Use contextual links permissions to edit blocks without visiting a separate page. For more information, see the online documentation for the Settings Tray module.', [':handbook_url' => 'https://www.drupal.org/documentation/modules/settings_tray', ':administer_block_permission' => Url::fromRoute('user.admin_permissions', [], ['fragment' => 'module-block'])->toString(), ':contextual_permission' => Url::fromRoute('user.admin_permissions', [], ['fragment' => 'module-contextual'])->toString()]) . '

'; Chris@0: $output .= '

' . t('Uses') . '

'; Chris@0: $output .= '
'; Chris@14: $output .= '
' . t('Editing blocks in place') . '
'; Chris@14: $output .= '
'; Chris@18: $output .= '

' . t('To edit blocks in place, either click the Edit button in the toolbar and then click on the block, or choose "Quick edit" from the block\'s contextual link. (See the Contextual Links module help for more information about how to use contextual links.)', [':contextual' => Url::fromRoute('help.page', ['name' => 'contextual'])->toString()]) . '

'; Chris@14: $output .= '

' . t('The Settings Tray for the block will open in a sidebar, with a compact form for configuring what the block shows.') . '

'; Chris@14: $output .= '

' . t('Save the form and the changes will be immediately visible on the page.') . '

'; Chris@14: $output .= '
'; Chris@0: $output .= '
'; Chris@0: return ['#markup' => $output]; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Implements hook_contextual_links_view_alter(). Chris@0: * Chris@0: * Change Configure Blocks into off_canvas links. Chris@0: */ Chris@0: function settings_tray_contextual_links_view_alter(&$element, $items) { Chris@0: if (isset($element['#links']['settings-trayblock-configure'])) { Chris@0: // Place settings_tray link first. Chris@0: $settings_tray_link = $element['#links']['settings-trayblock-configure']; Chris@0: unset($element['#links']['settings-trayblock-configure']); Chris@0: $element['#links'] = ['settings-trayblock-configure' => $settings_tray_link] + $element['#links']; Chris@0: Chris@0: // If this is content block change title to avoid duplicate "Quick Edit". Chris@0: if (isset($element['#links']['block-contentblock-edit'])) { Chris@0: $element['#links']['settings-trayblock-configure']['title'] = t('Quick edit settings'); Chris@0: } Chris@0: Chris@14: $element['#attached']['library'][] = 'core/drupal.dialog.off_canvas'; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@14: * Checks if a block has overrides. Chris@14: * Chris@14: * @param \Drupal\block\BlockInterface $block Chris@14: * The block to check for overrides. Chris@14: * Chris@14: * @return bool Chris@14: * TRUE if the block has overrides otherwise FALSE. Chris@14: * Chris@14: * @internal Chris@14: */ Chris@14: function _settings_tray_has_block_overrides(BlockInterface $block) { Chris@14: // @todo Replace the following with $block->hasOverrides() in https://www.drupal.org/project/drupal/issues/2910353 Chris@14: // and remove this function. Chris@14: return \Drupal::config($block->getEntityType()->getConfigPrefix() . '.' . $block->id())->hasOverrides(); Chris@14: } Chris@14: Chris@14: /** Chris@0: * Implements hook_block_view_alter(). Chris@0: */ Chris@0: function settings_tray_block_view_alter(array &$build) { Chris@14: if (isset($build['#contextual_links']['block'])) { Chris@14: // Ensure that contextual links vary by whether the block has config overrides Chris@14: // or not. Chris@14: // @see _contextual_links_to_id() Chris@14: $build['#contextual_links']['block']['metadata']['has_overrides'] = _settings_tray_has_block_overrides($build['#block']) ? 1 : 0; Chris@14: } Chris@14: Chris@0: // Force a new 'data-contextual-id' attribute on blocks when this module is Chris@0: // enabled so as not to reuse stale data cached client-side. Chris@0: // @todo Remove when https://www.drupal.org/node/2773591 is fixed. Chris@0: $build['#contextual_links']['settings_tray'] = [ Chris@0: 'route_parameters' => [], Chris@0: ]; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Implements hook_entity_type_build(). Chris@0: */ Chris@0: function settings_tray_entity_type_build(array &$entity_types) { Chris@0: /* @var $entity_types \Drupal\Core\Entity\EntityTypeInterface[] */ Chris@0: $entity_types['block'] Chris@14: ->setFormClass('settings_tray', BlockEntitySettingTrayForm::class) Chris@14: ->setLinkTemplate('settings_tray-form', '/admin/structure/block/manage/{block}/settings-tray'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Implements hook_preprocess_HOOK() for block templates. Chris@0: */ Chris@0: function settings_tray_preprocess_block(&$variables) { Chris@14: // Only blocks that have a settings_tray form and have no configuration Chris@14: // overrides will have a "Quick Edit" link. We could wait for the contextual Chris@14: // links to be initialized on the client side, and then add the class and Chris@14: // data- attribute below there (via JavaScript). But that would mean that it Chris@14: // would be impossible to show Settings Tray's clickable regions immediately Chris@14: // when the page loads. When latency is high, this will cause flicker. Chris@0: // @see \Drupal\settings_tray\Access\BlockPluginHasSettingsTrayFormAccessCheck Chris@0: /** @var \Drupal\settings_tray\Access\BlockPluginHasSettingsTrayFormAccessCheck $access_checker */ Chris@0: $access_checker = \Drupal::service('access_check.settings_tray.block.settings_tray_form'); Chris@0: /** @var \Drupal\Core\Block\BlockManagerInterface $block_plugin_manager */ Chris@0: $block_plugin_manager = \Drupal::service('plugin.manager.block'); Chris@0: /** @var \Drupal\Core\Block\BlockPluginInterface $block_plugin */ Chris@0: $block_plugin = $block_plugin_manager->createInstance($variables['plugin_id']); Chris@14: if (isset($variables['elements']['#contextual_links']['block']['route_parameters']['block'])) { Chris@14: $block = Block::load($variables['elements']['#contextual_links']['block']['route_parameters']['block']); Chris@14: if ($access_checker->accessBlockPlugin($block_plugin)->isAllowed() && !_settings_tray_has_block_overrides($block)) { Chris@14: // Add class and attributes to all blocks to allow Javascript to target. Chris@14: $variables['attributes']['class'][] = 'settings-tray-editable'; Chris@14: $variables['attributes']['data-drupal-settingstray'] = 'editable'; Chris@14: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Implements hook_toolbar_alter(). Chris@0: * Chris@0: * Alters the 'contextual' toolbar tab if it exists (meaning the user is allowed Chris@0: * to use contextual links) and if they can administer blocks. Chris@0: * Chris@0: * @todo Remove the "administer blocks" requirement in Chris@0: * https://www.drupal.org/node/2822965. Chris@0: * Chris@0: * @see contextual_toolbar() Chris@0: */ Chris@0: function settings_tray_toolbar_alter(&$items) { Chris@0: $items['contextual']['#cache']['contexts'][] = 'user.permissions'; Chris@0: if (isset($items['contextual']['tab']) && \Drupal::currentUser()->hasPermission('administer blocks')) { Chris@0: $items['contextual']['#weight'] = -1000; Chris@0: $items['contextual']['#attached']['library'][] = 'settings_tray/drupal.settings_tray'; Chris@0: $items['contextual']['tab']['#attributes']['data-drupal-settingstray'] = 'toggle'; Chris@0: Chris@0: // Set a class on items to mark whether they should be active in edit mode. Chris@0: // @todo Create a dynamic method for modules to set their own items. Chris@0: // https://www.drupal.org/node/2784589. Chris@0: $edit_mode_items = ['contextual', 'block_place']; Chris@0: foreach ($items as $key => $item) { Chris@0: if (!in_array($key, $edit_mode_items) && (!isset($items[$key]['#wrapper_attributes']['class']) || !in_array('hidden', $items[$key]['#wrapper_attributes']['class']))) { Chris@0: $items[$key]['#wrapper_attributes']['class'][] = 'edit-mode-inactive'; Chris@0: } Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Implements hook_block_alter(). Chris@0: * Chris@0: * Ensures every block plugin definition has an 'settings_tray' form specified. Chris@0: * Chris@0: * @see \Drupal\settings_tray\Access\BlockPluginHasSettingsTrayFormAccessCheck Chris@0: */ Chris@0: function settings_tray_block_alter(&$definitions) { Chris@0: foreach ($definitions as &$definition) { Chris@14: // If a block plugin does not define its own 'settings_tray' form, use the Chris@14: // plugin class itself. Chris@14: if (!isset($definition['forms']['settings_tray'])) { Chris@14: $definition['forms']['settings_tray'] = $definition['class']; Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Implements hook_css_alter(). Chris@0: */ Chris@0: function settings_tray_css_alter(&$css, AttachedAssetsInterface $assets) { Chris@0: // @todo Remove once conditional ordering is introduced in Chris@0: // https://www.drupal.org/node/1945262. Chris@0: $path = drupal_get_path('module', 'settings_tray') . '/css/settings_tray.theme.css'; Chris@0: if (isset($css[$path])) { Chris@0: // Use 200 to come after CSS_AGGREGATE_THEME. Chris@0: $css[$path]['group'] = 200; Chris@0: } Chris@0: }