Issue #2753941 by tedbow, tim.plunkett, tkoleary, SKAUGHT, drpal, effulgentsia, andrewmacpherson, webchick, xjm, Bojhan, Gábor Hojtsy, Wim Leers, catch, nod_: [Experimental] Create Outside In module MVP to provide block configuration in Off-Canvas tray and expand site edit mode
2016-08-17 10:59:07 +00:00
<?php
/**
* @file
* Allows configuring blocks and other configuration from the site front-end.
*/
2016-09-20 19:25:10 +00:00
use Drupal\Core\Asset\AttachedAssetsInterface;
Issue #2753941 by tedbow, tim.plunkett, tkoleary, SKAUGHT, drpal, effulgentsia, andrewmacpherson, webchick, xjm, Bojhan, Gábor Hojtsy, Wim Leers, catch, nod_: [Experimental] Create Outside In module MVP to provide block configuration in Off-Canvas tray and expand site edit mode
2016-08-17 10:59:07 +00:00
use Drupal\Core\Routing\RouteMatchInterface;
2018-01-19 22:55:05 +00:00
use Drupal\block\entity\Block;
use Drupal\block\BlockInterface;
2018-02-01 19:13:08 +00:00
use Drupal\settings_tray\Block\BlockEntitySettingTrayForm;
Issue #2753941 by tedbow, tim.plunkett, tkoleary, SKAUGHT, drpal, effulgentsia, andrewmacpherson, webchick, xjm, Bojhan, Gábor Hojtsy, Wim Leers, catch, nod_: [Experimental] Create Outside In module MVP to provide block configuration in Off-Canvas tray and expand site edit mode
2016-08-17 10:59:07 +00:00
/**
* Implements hook_help().
*/
2017-08-28 16:06:25 +00:00
function settings_tray_help($route_name, RouteMatchInterface $route_match) {
Issue #2753941 by tedbow, tim.plunkett, tkoleary, SKAUGHT, drpal, effulgentsia, andrewmacpherson, webchick, xjm, Bojhan, Gábor Hojtsy, Wim Leers, catch, nod_: [Experimental] Create Outside In module MVP to provide block configuration in Off-Canvas tray and expand site edit mode
2016-08-17 10:59:07 +00:00
switch ($route_name) {
2017-08-28 16:06:25 +00:00
case 'help.page.settings_tray':
Issue #2753941 by tedbow, tim.plunkett, tkoleary, SKAUGHT, drpal, effulgentsia, andrewmacpherson, webchick, xjm, Bojhan, Gábor Hojtsy, Wim Leers, catch, nod_: [Experimental] Create Outside In module MVP to provide block configuration in Off-Canvas tray and expand site edit mode
2016-08-17 10:59:07 +00:00
$output = '<h3>' . t('About') . '</h3>';
2018-01-10 20:08:31 +00:00
$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>';
2016-09-19 11:57:00 +00:00
$output .= '<h3>' . t('Uses') . '</h3>';
$output .= '<dl>';
2018-01-10 20:08:31 +00:00
$output .= '<dt>' . t('Editing blocks in place') . '</dt>';
$output .= '<dd>';
$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>';
$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>';
$output .= '<p>' . t('Save the form and the changes will be immediately visible on the page.') . '</p>';
$output .= '</dd>';
2016-09-19 11:57:00 +00:00
$output .= '</dl>';
return ['#markup' => $output];
Issue #2753941 by tedbow, tim.plunkett, tkoleary, SKAUGHT, drpal, effulgentsia, andrewmacpherson, webchick, xjm, Bojhan, Gábor Hojtsy, Wim Leers, catch, nod_: [Experimental] Create Outside In module MVP to provide block configuration in Off-Canvas tray and expand site edit mode
2016-08-17 10:59:07 +00:00
}
}
/**
* Implements hook_contextual_links_view_alter().
*
2017-04-29 21:36:10 +00:00
* Change Configure Blocks into off_canvas links.
Issue #2753941 by tedbow, tim.plunkett, tkoleary, SKAUGHT, drpal, effulgentsia, andrewmacpherson, webchick, xjm, Bojhan, Gábor Hojtsy, Wim Leers, catch, nod_: [Experimental] Create Outside In module MVP to provide block configuration in Off-Canvas tray and expand site edit mode
2016-08-17 10:59:07 +00:00
*/
2017-08-28 16:06:25 +00:00
function settings_tray_contextual_links_view_alter(&$element, $items) {
if (isset($element['#links']['settings-trayblock-configure'])) {
// Place settings_tray link first.
$settings_tray_link = $element['#links']['settings-trayblock-configure'];
unset($element['#links']['settings-trayblock-configure']);
$element['#links'] = ['settings-trayblock-configure' => $settings_tray_link] + $element['#links'];
2017-04-15 10:19:10 +00:00
2017-04-15 19:28:39 +00:00
// If this is content block change title to avoid duplicate "Quick Edit".
if (isset($element['#links']['block-contentblock-edit'])) {
2017-08-28 16:06:25 +00:00
$element['#links']['settings-trayblock-configure']['title'] = t('Quick edit settings');
2017-04-15 19:28:39 +00:00
}
Issue #2753941 by tedbow, tim.plunkett, tkoleary, SKAUGHT, drpal, effulgentsia, andrewmacpherson, webchick, xjm, Bojhan, Gábor Hojtsy, Wim Leers, catch, nod_: [Experimental] Create Outside In module MVP to provide block configuration in Off-Canvas tray and expand site edit mode
2016-08-17 10:59:07 +00:00
2017-10-24 22:46:32 +00:00
$element['#attached']['library'][] = 'core/drupal.dialog.off_canvas';
Issue #2753941 by tedbow, tim.plunkett, tkoleary, SKAUGHT, drpal, effulgentsia, andrewmacpherson, webchick, xjm, Bojhan, Gábor Hojtsy, Wim Leers, catch, nod_: [Experimental] Create Outside In module MVP to provide block configuration in Off-Canvas tray and expand site edit mode
2016-08-17 10:59:07 +00:00
}
}
2018-01-19 22:55:05 +00:00
/**
* Checks if a block has overrides.
*
* @param \Drupal\block\BlockInterface $block
* The block to check for overrides.
*
* @return bool
* TRUE if the block has overrides otherwise FALSE.
*
* @internal
*/
function _settings_tray_has_block_overrides(BlockInterface $block) {
// @todo Replace the following with $block->hasOverrides() in https://www.drupal.org/project/drupal/issues/2910353
// and remove this function.
return \Drupal::config($block->getEntityType()->getConfigPrefix() . '.' . $block->id())->hasOverrides();
}
Issue #2753941 by tedbow, tim.plunkett, tkoleary, SKAUGHT, drpal, effulgentsia, andrewmacpherson, webchick, xjm, Bojhan, Gábor Hojtsy, Wim Leers, catch, nod_: [Experimental] Create Outside In module MVP to provide block configuration in Off-Canvas tray and expand site edit mode
2016-08-17 10:59:07 +00:00
/**
* Implements hook_block_view_alter().
*/
2017-08-28 16:06:25 +00:00
function settings_tray_block_view_alter(array &$build) {
2018-01-19 22:55:05 +00:00
if (isset($build['#contextual_links']['block'])) {
// Ensure that contextual links vary by whether the block has config overrides
// or not.
// @see _contextual_links_to_id()
$build['#contextual_links']['block']['metadata']['has_overrides'] = _settings_tray_has_block_overrides($build['#block']) ? 1 : 0;
}
Issue #2753941 by tedbow, tim.plunkett, tkoleary, SKAUGHT, drpal, effulgentsia, andrewmacpherson, webchick, xjm, Bojhan, Gábor Hojtsy, Wim Leers, catch, nod_: [Experimental] Create Outside In module MVP to provide block configuration in Off-Canvas tray and expand site edit mode
2016-08-17 10:59:07 +00:00
// Force a new 'data-contextual-id' attribute on blocks when this module is
// enabled so as not to reuse stale data cached client-side.
// @todo Remove when https://www.drupal.org/node/2773591 is fixed.
2017-08-28 16:06:25 +00:00
$build['#contextual_links']['settings_tray'] = [
Issue #2753941 by tedbow, tim.plunkett, tkoleary, SKAUGHT, drpal, effulgentsia, andrewmacpherson, webchick, xjm, Bojhan, Gábor Hojtsy, Wim Leers, catch, nod_: [Experimental] Create Outside In module MVP to provide block configuration in Off-Canvas tray and expand site edit mode
2016-08-17 10:59:07 +00:00
'route_parameters' => [],
];
}
/**
* Implements hook_entity_type_build().
*/
2017-08-28 16:06:25 +00:00
function settings_tray_entity_type_build(array &$entity_types) {
Issue #2753941 by tedbow, tim.plunkett, tkoleary, SKAUGHT, drpal, effulgentsia, andrewmacpherson, webchick, xjm, Bojhan, Gábor Hojtsy, Wim Leers, catch, nod_: [Experimental] Create Outside In module MVP to provide block configuration in Off-Canvas tray and expand site edit mode
2016-08-17 10:59:07 +00:00
/* @var $entity_types \Drupal\Core\Entity\EntityTypeInterface[] */
$entity_types['block']
2018-02-01 19:13:08 +00:00
->setFormClass('settings_tray', BlockEntitySettingTrayForm::class)
->setLinkTemplate('settings_tray-form', '/admin/structure/block/manage/{block}/settings-tray');
Issue #2753941 by tedbow, tim.plunkett, tkoleary, SKAUGHT, drpal, effulgentsia, andrewmacpherson, webchick, xjm, Bojhan, Gábor Hojtsy, Wim Leers, catch, nod_: [Experimental] Create Outside In module MVP to provide block configuration in Off-Canvas tray and expand site edit mode
2016-08-17 10:59:07 +00:00
}
/**
* Implements hook_preprocess_HOOK() for block templates.
*/
2017-08-28 16:06:25 +00:00
function settings_tray_preprocess_block(&$variables) {
2018-01-19 22:55:05 +00:00
// Only blocks that have a settings_tray form and have no configuration
// overrides will have a "Quick Edit" link. We could wait for the contextual
// links to be initialized on the client side, and then add the class and
// data- attribute below there (via JavaScript). But that would mean that it
// would be impossible to show Settings Tray's clickable regions immediately
// when the page loads. When latency is high, this will cause flicker.
2017-08-28 16:06:25 +00:00
// @see \Drupal\settings_tray\Access\BlockPluginHasSettingsTrayFormAccessCheck
/** @var \Drupal\settings_tray\Access\BlockPluginHasSettingsTrayFormAccessCheck $access_checker */
$access_checker = \Drupal::service('access_check.settings_tray.block.settings_tray_form');
2017-08-18 20:48:28 +00:00
/** @var \Drupal\Core\Block\BlockManagerInterface $block_plugin_manager */
$block_plugin_manager = \Drupal::service('plugin.manager.block');
/** @var \Drupal\Core\Block\BlockPluginInterface $block_plugin */
$block_plugin = $block_plugin_manager->createInstance($variables['plugin_id']);
2018-01-19 22:55:05 +00:00
if (isset($variables['elements']['#contextual_links']['block']['route_parameters']['block'])) {
$block = Block::load($variables['elements']['#contextual_links']['block']['route_parameters']['block']);
if ($access_checker->accessBlockPlugin($block_plugin)->isAllowed() && !_settings_tray_has_block_overrides($block)) {
// Add class and attributes to all blocks to allow Javascript to target.
$variables['attributes']['class'][] = 'settings-tray-editable';
$variables['attributes']['data-drupal-settingstray'] = 'editable';
}
Issue #2753941 by tedbow, tim.plunkett, tkoleary, SKAUGHT, drpal, effulgentsia, andrewmacpherson, webchick, xjm, Bojhan, Gábor Hojtsy, Wim Leers, catch, nod_: [Experimental] Create Outside In module MVP to provide block configuration in Off-Canvas tray and expand site edit mode
2016-08-17 10:59:07 +00:00
}
}
/**
* Implements hook_toolbar_alter().
*
2017-07-19 21:12:03 +00:00
* Alters the 'contextual' toolbar tab if it exists (meaning the user is allowed
* to use contextual links) and if they can administer blocks.
*
2017-08-18 20:48:28 +00:00
* @todo Remove the "administer blocks" requirement in
* https://www.drupal.org/node/2822965.
*
2017-07-19 21:12:03 +00:00
* @see contextual_toolbar()
Issue #2753941 by tedbow, tim.plunkett, tkoleary, SKAUGHT, drpal, effulgentsia, andrewmacpherson, webchick, xjm, Bojhan, Gábor Hojtsy, Wim Leers, catch, nod_: [Experimental] Create Outside In module MVP to provide block configuration in Off-Canvas tray and expand site edit mode
2016-08-17 10:59:07 +00:00
*/
2017-08-28 16:06:25 +00:00
function settings_tray_toolbar_alter(&$items) {
2017-07-19 21:12:03 +00:00
$items['contextual']['#cache']['contexts'][] = 'user.permissions';
if (isset($items['contextual']['tab']) && \Drupal::currentUser()->hasPermission('administer blocks')) {
Issue #2753941 by tedbow, tim.plunkett, tkoleary, SKAUGHT, drpal, effulgentsia, andrewmacpherson, webchick, xjm, Bojhan, Gábor Hojtsy, Wim Leers, catch, nod_: [Experimental] Create Outside In module MVP to provide block configuration in Off-Canvas tray and expand site edit mode
2016-08-17 10:59:07 +00:00
$items['contextual']['#weight'] = -1000;
2017-08-28 16:06:25 +00:00
$items['contextual']['#attached']['library'][] = 'settings_tray/drupal.settings_tray';
$items['contextual']['tab']['#attributes']['data-drupal-settingstray'] = 'toggle';
Issue #2753941 by tedbow, tim.plunkett, tkoleary, SKAUGHT, drpal, effulgentsia, andrewmacpherson, webchick, xjm, Bojhan, Gábor Hojtsy, Wim Leers, catch, nod_: [Experimental] Create Outside In module MVP to provide block configuration in Off-Canvas tray and expand site edit mode
2016-08-17 10:59:07 +00:00
// Set a class on items to mark whether they should be active in edit mode.
// @todo Create a dynamic method for modules to set their own items.
2017-08-18 20:48:28 +00:00
// https://www.drupal.org/node/2784589.
Issue #2753941 by tedbow, tim.plunkett, tkoleary, SKAUGHT, drpal, effulgentsia, andrewmacpherson, webchick, xjm, Bojhan, Gábor Hojtsy, Wim Leers, catch, nod_: [Experimental] Create Outside In module MVP to provide block configuration in Off-Canvas tray and expand site edit mode
2016-08-17 10:59:07 +00:00
$edit_mode_items = ['contextual', 'block_place'];
foreach ($items as $key => $item) {
if (!in_array($key, $edit_mode_items) && (!isset($items[$key]['#wrapper_attributes']['class']) || !in_array('hidden', $items[$key]['#wrapper_attributes']['class']))) {
$items[$key]['#wrapper_attributes']['class'][] = 'edit-mode-inactive';
}
}
}
}
/**
* Implements hook_block_alter().
2017-08-18 20:48:28 +00:00
*
2017-08-25 19:41:33 +00:00
* Ensures every block plugin definition has an 'settings_tray' form specified.
2017-08-18 20:48:28 +00:00
*
2017-08-28 16:06:25 +00:00
* @see \Drupal\settings_tray\Access\BlockPluginHasSettingsTrayFormAccessCheck
Issue #2753941 by tedbow, tim.plunkett, tkoleary, SKAUGHT, drpal, effulgentsia, andrewmacpherson, webchick, xjm, Bojhan, Gábor Hojtsy, Wim Leers, catch, nod_: [Experimental] Create Outside In module MVP to provide block configuration in Off-Canvas tray and expand site edit mode
2016-08-17 10:59:07 +00:00
*/
2017-08-28 16:06:25 +00:00
function settings_tray_block_alter(&$definitions) {
Issue #2753941 by tedbow, tim.plunkett, tkoleary, SKAUGHT, drpal, effulgentsia, andrewmacpherson, webchick, xjm, Bojhan, Gábor Hojtsy, Wim Leers, catch, nod_: [Experimental] Create Outside In module MVP to provide block configuration in Off-Canvas tray and expand site edit mode
2016-08-17 10:59:07 +00:00
foreach ($definitions as &$definition) {
2018-01-29 22:38:57 +00:00
// If a block plugin does not define its own 'settings_tray' form, use the
// plugin class itself.
if (!isset($definition['forms']['settings_tray'])) {
$definition['forms']['settings_tray'] = $definition['class'];
Issue #2753941 by tedbow, tim.plunkett, tkoleary, SKAUGHT, drpal, effulgentsia, andrewmacpherson, webchick, xjm, Bojhan, Gábor Hojtsy, Wim Leers, catch, nod_: [Experimental] Create Outside In module MVP to provide block configuration in Off-Canvas tray and expand site edit mode
2016-08-17 10:59:07 +00:00
}
}
}
2016-09-20 19:25:10 +00:00
/**
* Implements hook_css_alter().
*/
2017-08-28 16:06:25 +00:00
function settings_tray_css_alter(&$css, AttachedAssetsInterface $assets) {
2016-09-20 19:25:10 +00:00
// @todo Remove once conditional ordering is introduced in
// https://www.drupal.org/node/1945262.
2017-08-28 16:06:25 +00:00
$path = drupal_get_path('module', 'settings_tray') . '/css/settings_tray.theme.css';
2016-09-20 19:25:10 +00:00
if (isset($css[$path])) {
// Use 200 to come after CSS_AGGREGATE_THEME.
$css[$path]['group'] = 200;
}
}