diff --git a/core/modules/settings_tray/js/settings_tray.es6.js b/core/modules/settings_tray/js/settings_tray.es6.js index a680115490..c182a873ed 100644 --- a/core/modules/settings_tray/js/settings_tray.es6.js +++ b/core/modules/settings_tray/js/settings_tray.es6.js @@ -5,7 +5,7 @@ * @private */ -(function ($, Drupal) { +(function ($, Drupal, drupalSettings) { const blockConfigureSelector = '[data-settings-tray-edit]'; const toggleEditSelector = '[data-drupal-settingstray="toggle"]'; const itemsToToggleSelector = '[data-off-canvas-main-canvas], #toolbar-bar, [data-drupal-settingstray="editable"] a, [data-drupal-settingstray="editable"] button'; @@ -164,6 +164,17 @@ if (!('dialogOptions' in instance.options.data)) { instance.options.data.dialogOptions = {}; } + + if (drupalSettings.hasOwnProperty('settings_tray') && drupalSettings.settings_tray.hasOwnProperty('overridden_blocks')) { + Object.entries(drupalSettings.settings_tray.overridden_blocks).forEach( + ([blockId, url]) => { + if (instance.options.url.includes(`/${blockId}/`)) { + instance.options.url = url; + } + }, + ); + } + instance.options.data.dialogOptions.settingsTrayActiveEditableId = $(instance.element).parents('.settings-tray-editable').attr('id'); instance.progress = { type: 'fullscreen' }; }); @@ -253,4 +264,4 @@ } }, }); -}(jQuery, Drupal)); +}(jQuery, Drupal, drupalSettings)); diff --git a/core/modules/settings_tray/js/settings_tray.js b/core/modules/settings_tray/js/settings_tray.js index 78c5b60382..98e81f3d93 100644 --- a/core/modules/settings_tray/js/settings_tray.js +++ b/core/modules/settings_tray/js/settings_tray.js @@ -4,8 +4,9 @@ * https://www.drupal.org/node/2815083 * @preserve **/ +var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }(); -(function ($, Drupal) { +(function ($, Drupal, drupalSettings) { var blockConfigureSelector = '[data-settings-tray-edit]'; var toggleEditSelector = '[data-drupal-settingstray="toggle"]'; var itemsToToggleSelector = '[data-off-canvas-main-canvas], #toolbar-bar, [data-drupal-settingstray="editable"] a, [data-drupal-settingstray="editable"] button'; @@ -100,6 +101,19 @@ if (!('dialogOptions' in instance.options.data)) { instance.options.data.dialogOptions = {}; } + + if (drupalSettings.hasOwnProperty('settings_tray') && drupalSettings.settings_tray.hasOwnProperty('overridden_blocks')) { + Object.entries(drupalSettings.settings_tray.overridden_blocks).forEach(function (_ref) { + var _ref2 = _slicedToArray(_ref, 2), + blockId = _ref2[0], + url = _ref2[1]; + + if (instance.options.url.includes('/' + blockId + '/')) { + instance.options.url = url; + } + }); + } + instance.options.data.dialogOptions.settingsTrayActiveEditableId = $(instance.element).parents('.settings-tray-editable').attr('id'); instance.progress = { type: 'fullscreen' }; }); @@ -153,4 +167,4 @@ } } }); -})(jQuery, Drupal); \ No newline at end of file +})(jQuery, Drupal, drupalSettings); \ No newline at end of file diff --git a/core/modules/settings_tray/settings_tray.libraries.yml b/core/modules/settings_tray/settings_tray.libraries.yml index de5c82c78d..11b55a478f 100644 --- a/core/modules/settings_tray/settings_tray.libraries.yml +++ b/core/modules/settings_tray/settings_tray.libraries.yml @@ -17,3 +17,4 @@ drupal.settings_tray: - core/drupal - core/jquery.once - core/drupal.ajax + - core/drupalSettings diff --git a/core/modules/settings_tray/settings_tray.module b/core/modules/settings_tray/settings_tray.module index af04450459..821f62c19c 100644 --- a/core/modules/settings_tray/settings_tray.module +++ b/core/modules/settings_tray/settings_tray.module @@ -10,6 +10,9 @@ use Drupal\settings_tray\Block\BlockEntityOffCanvasForm; use Drupal\settings_tray\Form\SystemBrandingOffCanvasForm; use Drupal\settings_tray\Form\SystemMenuOffCanvasForm; +use Drupal\block\Entity\Block; +use Drupal\Core\Url; +use Drupal\Core\EventSubscriber\MainContentViewSubscriber; /** * Implements hook_help(). @@ -48,6 +51,21 @@ function settings_tray_contextual_links_view_alter(&$element, $items) { } } +/** + * Checks if a block has overrides. + * + * @param string $block_id + * The ID of the block to check for overrides. + * + * @return bool + * TRUE if the block has overrides otherwise FALSE. + */ +function _settings_tray_has_block_overrides($block_id) { + $block = Block::load($block_id); + $overrides = \Drupal::config($block->getEntityType()->getConfigPrefix() . '.' . $block->id())->getOverrides(); + return !empty($overrides); +} + /** * Implements hook_block_view_alter(). */ @@ -58,6 +76,27 @@ function settings_tray_block_view_alter(array &$build) { $build['#contextual_links']['settings_tray'] = [ 'route_parameters' => [], ]; + $block_id = $build['#block']->id(); + // If a block currently has configuration overrides it cannot be edited in the + // Settings Tray form. + if (_settings_tray_has_block_overrides($block_id)) { + $url = Url::fromRoute('settings_tray.overridden_block_warning') + ->setRouteParameter('block', $block_id) + ->setOptions( + [ + 'query' => [ + MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_dialog.off_canvas', + ] + \Drupal::destination()->getAsArray(), + ] + ); + // Store the URL to the overridden notice for this block in drupalSettings. + // This will be used in the Javascript function prepareAjaxLinks() to + // replace the URL to the Settings Tray edit form. We cannot use + // settings_tray_contextual_links_view_alter() to alter the URL because the + // contextual links will not be rebuilt for every context that could have + // configuration overrides. + $build['#attached']['drupalSettings']['settings_tray']['overridden_blocks'][$block_id] = $url->toString(); + } } /** diff --git a/core/modules/settings_tray/settings_tray.routing.yml b/core/modules/settings_tray/settings_tray.routing.yml index 01109e4c79..c2f4f7c140 100644 --- a/core/modules/settings_tray/settings_tray.routing.yml +++ b/core/modules/settings_tray/settings_tray.routing.yml @@ -6,3 +6,10 @@ entity.block.off_canvas_form: requirements: _permission: 'administer blocks' _access_block_plugin_has_settings_tray_form: 'TRUE' +settings_tray.overridden_block_warning: + path: '/admin/settings-tray-overridden/{block}' + defaults: + _controller: '\Drupal\settings_tray\Controller\OverriddenBlockConfig::overrideNotice' + _title_callback: '\Drupal\settings_tray\Block\BlockEntityOffCanvasForm::title' + requirements: + _permission: 'administer blocks' diff --git a/core/modules/settings_tray/src/Controller/OverriddenBlockConfig.php b/core/modules/settings_tray/src/Controller/OverriddenBlockConfig.php new file mode 100644 index 0000000000..f113f82070 --- /dev/null +++ b/core/modules/settings_tray/src/Controller/OverriddenBlockConfig.php @@ -0,0 +1,48 @@ + [ + '#type' => 'markup', + '#markup' => '

' . $this->t('This block cannot be edited in the Settings Tray form because it has configuration overrides in effect.') . '

', + ], + 'link' => [ + '#type' => 'link', + '#title' => $this->t('Edit Block'), + '#url' => Url::fromRoute('entity.block.edit_form') + ->setRouteParameter('block', $block->id()) + ->setOption('query', ['destination' => \Drupal::request()->get('destination')]), + ], + ]; + + } + +}