diff --git a/core/modules/settings_tray/js/settings_tray.es6.js b/core/modules/settings_tray/js/settings_tray.es6.js index a680115490..a50f45198d 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.keys(drupalSettings.settings_tray.overridden_blocks).forEach( + (blockId) => { + if (instance.options.url.indexOf(`/admin/structure/block/manage/${blockId}/off-canvas`) !== -1) { + instance.options.url = drupalSettings.settings_tray.overridden_blocks[blockId]; + } + }, + ); + } + 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..9d7e80a1bf 100644 --- a/core/modules/settings_tray/js/settings_tray.js +++ b/core/modules/settings_tray/js/settings_tray.js @@ -5,7 +5,7 @@ * @preserve **/ -(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 +100,15 @@ if (!('dialogOptions' in instance.options.data)) { instance.options.data.dialogOptions = {}; } + + if (drupalSettings.hasOwnProperty('settings_tray') && drupalSettings.settings_tray.hasOwnProperty('overridden_blocks')) { + Object.keys(drupalSettings.settings_tray.overridden_blocks).forEach(function (blockId) { + if (instance.options.url.indexOf('/admin/structure/block/manage/' + blockId + '/off-canvas') !== -1) { + instance.options.url = drupalSettings.settings_tray.overridden_blocks[blockId]; + } + }); + } + instance.options.data.dialogOptions.settingsTrayActiveEditableId = $(instance.element).parents('.settings-tray-editable').attr('id'); instance.progress = { type: 'fullscreen' }; }); @@ -153,4 +162,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')]), + ], + ]; + + } + +} diff --git a/core/modules/settings_tray/tests/modules/settings_tray_override_test/settings_tray_override_test.info.yml b/core/modules/settings_tray/tests/modules/settings_tray_override_test/settings_tray_override_test.info.yml new file mode 100644 index 0000000000..39fb56fa55 --- /dev/null +++ b/core/modules/settings_tray/tests/modules/settings_tray_override_test/settings_tray_override_test.info.yml @@ -0,0 +1,8 @@ +name: 'Configuration override test for Settings Tray' +type: module +package: Testing +version: VERSION +core: 8.x + +dependencies: + - settings_tray diff --git a/core/modules/settings_tray/tests/modules/settings_tray_override_test/settings_tray_override_test.services.yml b/core/modules/settings_tray/tests/modules/settings_tray_override_test/settings_tray_override_test.services.yml new file mode 100644 index 0000000000..b23035bde2 --- /dev/null +++ b/core/modules/settings_tray/tests/modules/settings_tray_override_test/settings_tray_override_test.services.yml @@ -0,0 +1,5 @@ +services: + settings_tray_override_test.overrider: + class: Drupal\settings_tray_override_test\ConfigOverrider + tags: + - { name: config.factory.override} diff --git a/core/modules/settings_tray/tests/modules/settings_tray_override_test/src/ConfigOverrider.php b/core/modules/settings_tray/tests/modules/settings_tray_override_test/src/ConfigOverrider.php new file mode 100644 index 0000000000..1ee59631c5 --- /dev/null +++ b/core/modules/settings_tray/tests/modules/settings_tray_override_test/src/ConfigOverrider.php @@ -0,0 +1,51 @@ + ['settings' => ['label' => 'ALL YOUR LABELS BELONG TO US']]]; + } + } + return $overrides; + } + + /** + * {@inheritdoc} + */ + public function getCacheSuffix() { + return 'ConfigOverrider'; + } + + /** + * {@inheritdoc} + */ + public function createConfigObject($name, $collection = StorageInterface::DEFAULT_COLLECTION) { + return NULL; + } + + /** + * {@inheritdoc} + */ + public function getCacheableMetadata($name) { + return new CacheableMetadata(); + } + +} diff --git a/core/modules/settings_tray/tests/src/FunctionalJavascript/SettingsTrayBlockFormTest.php b/core/modules/settings_tray/tests/src/FunctionalJavascript/SettingsTrayBlockFormTest.php index fcfecde400..5480fd512b 100644 --- a/core/modules/settings_tray/tests/src/FunctionalJavascript/SettingsTrayBlockFormTest.php +++ b/core/modules/settings_tray/tests/src/FunctionalJavascript/SettingsTrayBlockFormTest.php @@ -2,6 +2,7 @@ namespace Drupal\Tests\settings_tray\FunctionalJavascript; +use Drupal\block\BlockInterface; use Drupal\block\Entity\Block; use Drupal\block_content\Entity\BlockContent; use Drupal\block_content\Entity\BlockContentType; @@ -75,7 +76,7 @@ public function testBlocks($theme, $block_plugin, $new_page_text, $element_selec $page = $this->getSession()->getPage(); $this->enableTheme($theme); $block = $this->placeBlock($block_plugin); - $block_selector = str_replace('_', '-', $this->getBlockSelector($block)); + $block_selector = $this->getBlockCssSelector($block); $block_id = $block->id(); $this->drupalGet('user'); @@ -267,8 +268,10 @@ protected function assertOffCanvasBlockFormIsValid() { * @param string $contextual_link_container * The element that contains the contextual links. If none provide the * $block_selector will be used. + * @param bool $confirm_form + * Determines if the block form should be confirmed. */ - protected function openBlockForm($block_selector, $contextual_link_container = '') { + protected function openBlockForm($block_selector, $contextual_link_container = '', $confirm_form = TRUE) { if (!$contextual_link_container) { $contextual_link_container = $block_selector; } @@ -283,7 +286,10 @@ protected function openBlockForm($block_selector, $contextual_link_container = ' $this->assertSession()->assertWaitOnAjaxRequest(); $this->click($block_selector); $this->waitForOffCanvasToOpen(); - $this->assertOffCanvasBlockFormIsValid(); + if ($confirm_form) { + $this->assertOffCanvasBlockFormIsValid(); + } + } /** @@ -321,7 +327,7 @@ public function testQuickEditLinks() { $this->enableTheme($theme); $block = $this->placeBlock($block_plugin); - $block_selector = str_replace('_', '-', $this->getBlockSelector($block)); + $block_selector = $this->getBlockCssSelector($block); // Load the same page twice. foreach ([1, 2] as $page_load_times) { $this->drupalGet('node/' . $node->id()); @@ -577,4 +583,52 @@ protected function getTestThemes() { }); } + /** + * Tests that the blocks with configuration overrides are disabled. + */ + public function testOverriddenDisabled() { + $web_assert = $this->assertSession(); + $overridden_text = 'This block cannot be edited in the Settings Tray form because it has configuration overrides in effect.'; + $this->container->get('module_installer')->install(['settings_tray_override_test']); + // Test a overridden block does not show the form in the off-canvas dialog. + // @see \Drupal\settings_tray_override_test\ConfigOverrider + $overridden_block = $this->placeBlock('system_powered_by_block', + [ + 'id' => 'overridden_block', + 'label_display' => 1, + 'label' => 'This will be overridden.', + ]); + $this->drupalGet('user'); + // Confirm the label is actually overridden. + $web_assert->elementContains('css', $this->getBlockCssSelector($overridden_block), 'ALL YOUR LABELS BELONG TO US'); + $this->enableEditMode(); + $this->openBlockForm($this->getBlockCssSelector($overridden_block), '', FALSE); + $web_assert->elementContains('css', self::OFF_CANVAS_CSS_SELECTOR, $overridden_text); + + // Test a non-overridden block does show the form in the off-canvas dialog. + $block = $this->placeBlock('system_powered_by_block', + [ + 'label_display' => 1, + 'label' => 'Labely label', + ]); + $this->drupalGet('user'); + // Confirm the label is not overridden. + $web_assert->elementContains('css', $this->getBlockCssSelector($block), 'Labely label'); + $this->openBlockForm($this->getBlockCssSelector($block)); + $web_assert->elementNotContains('css', self::OFF_CANVAS_CSS_SELECTOR, $overridden_text); + } + + /** + * Gets the CSS selector for a block. + * + * @param \Drupal\block\BlockInterface $block + * The block. + * + * @return string + * The CSS selector for the block. + */ + protected function getBlockCssSelector(BlockInterface $block) { + return str_replace('_', '-', $this->getBlockSelector($block)); + } + }