diff --git a/core/modules/block_place/block_place.libraries.yml b/core/modules/block_place/block_place.libraries.yml index 0671252..7d19419 100644 --- a/core/modules/block_place/block_place.libraries.yml +++ b/core/modules/block_place/block_place.libraries.yml @@ -9,3 +9,11 @@ drupal.block_place.icons: css: theme: css/block-place.icons.theme.css: {} + +drupal.block_place.js: + version: VERSION + js: + js/block_place.js: {} + dependencies: + - outside_in/ drupal.outside_in + diff --git a/core/modules/block_place/block_place.module b/core/modules/block_place/block_place.module index 7fe86a6..9083f75 100644 --- a/core/modules/block_place/block_place.module +++ b/core/modules/block_place/block_place.module @@ -82,3 +82,22 @@ function block_place_toolbar() { ]; return $items; } + +/** + * Implements hook_page_top(). + * + * Add block place library if dialog should be open to sort blocks. + */ +function block_place_page_top(array &$page_top) { + if ($region_sort = \Drupal::request()->query->get('region_sort')) { + $page_top['#attached']['library'][] = 'block_place/drupal.block_place.js'; + $destination = \Drupal::destination()->get(); + $destination = explode('?', $destination)[0]; + $url = Url::fromRoute('block_place.admin_display', ['region' => $region_sort], ['query' => ['destination' => $destination]]); + $page_top['#attached']['drupalSettings']['block_place'] = [ + 'dialog_url' => $url->setAbsolute()->toString(), + 'dialog_type' => \Drupal::moduleHandler()->moduleExists('outside_in') ? 'dialog_offcanvas' : 'modal', + ]; + } +} + diff --git a/core/modules/block_place/block_place.routing.yml b/core/modules/block_place/block_place.routing.yml new file mode 100644 index 0000000..9c4a4d8 --- /dev/null +++ b/core/modules/block_place/block_place.routing.yml @@ -0,0 +1,15 @@ +block_place.admin_library: + path: 'admin/structure/block-place/library/{theme}' + defaults: + _controller: '\Drupal\block_place\Controller\PlaceBlockLibraryController::listBlocks' + _title: 'Place block' + requirements: + _access_theme: 'TRUE' + _permission: 'administer blocks' +block_place.admin_display: + path: 'admin/structure/block-place/{region}' + defaults: + _controller: '\Drupal\block_place\Controller\BlockPlaceListController::listing' + _title: 'Order Blocks' + requirements: + _permission: 'administer blocks' diff --git a/core/modules/block_place/js/block_place.js b/core/modules/block_place/js/block_place.js new file mode 100644 index 0000000..140b7c5 --- /dev/null +++ b/core/modules/block_place/js/block_place.js @@ -0,0 +1,29 @@ +/** + * @file + * Block Place behaviors. + */ + +(function ($, window, Drupal, drupalSettings) { + + 'use strict'; + + Drupal.behaviors.blockPlace = { + attach: function (context, settings) { + // If drupalSettings.block_place is set open open dialog. + if (drupalSettings.hasOwnProperty('block_place') && drupalSettings.block_place.hasOwnProperty('dialog_url')) { + $(window).once('block_sort').each(function () { + var blockSort = Drupal.ajax({ + dialog: {}, + dialogType: drupalSettings.block_place.dialog_type, + selector: '.ckeditor-dialog-loading-link', + url: drupalSettings.block_place.dialog_url, + progress: {type: 'throbber'} + }); + blockSort.execute(); + }); + } + + } + }; + +})(jQuery, window, Drupal, drupalSettings); diff --git a/core/modules/block_place/src/Controller/BlockPlaceListController.php b/core/modules/block_place/src/Controller/BlockPlaceListController.php new file mode 100644 index 0000000..00e6384 --- /dev/null +++ b/core/modules/block_place/src/Controller/BlockPlaceListController.php @@ -0,0 +1,55 @@ + 'value', + '#value' => $region, + ]; + $rendered['blocks'][$key]['theme'] = [ + '#type' => 'value', + '#value' => $theme, + ]; + } + } + + } + // Unset row to display region name and place a new block. + unset($rendered['blocks']["region-$region"], $rendered['blocks']["region-$region-message"]); + return $rendered; + } + +} diff --git a/core/modules/block_place/src/Controller/PlaceBlockLibraryController.php b/core/modules/block_place/src/Controller/PlaceBlockLibraryController.php new file mode 100644 index 0000000..9aa2847 --- /dev/null +++ b/core/modules/block_place/src/Controller/PlaceBlockLibraryController.php @@ -0,0 +1,45 @@ +moduleHandler()->moduleExists('outside_in')) { + $data_dialog_attributes = [ + 'data-dialog-type' => 'dialog', + 'data-dialog-renderer' => 'offcanvas', + 'data-dialog-options' => Json::encode([ + 'width' => 425, + ]), + ]; + } + else { + $data_dialog_attributes = []; + } + foreach ($build['blocks']['#rows'] as &$row) { + if (isset($row['operations']['data']['#links']['add'])) { + $row['operations']['data']['#links']['add']['attributes'] = $data_dialog_attributes + $row['operations']['data']['#links']['add']['attributes']; + $row['operations']['data']['#links']['add']['query']['destination'] = \Drupal::destination()->get(); + } + } + } + return $build; + } + +} diff --git a/core/modules/block_place/src/Plugin/DisplayVariant/PlaceBlockPageVariant.php b/core/modules/block_place/src/Plugin/DisplayVariant/PlaceBlockPageVariant.php index d05c7f1..a13a315 100644 --- a/core/modules/block_place/src/Plugin/DisplayVariant/PlaceBlockPageVariant.php +++ b/core/modules/block_place/src/Plugin/DisplayVariant/PlaceBlockPageVariant.php @@ -6,9 +6,10 @@ use Drupal\block\Plugin\DisplayVariant\BlockPageVariant; use Drupal\Component\Serialization\Json; use Drupal\Core\Entity\EntityViewBuilderInterface; +use Drupal\Core\Extension\ModuleHandlerInterface; +use Drupal\Core\Link; use Drupal\Core\Routing\RedirectDestinationInterface; use Drupal\Core\Theme\ThemeManagerInterface; -use Drupal\Core\Link; use Symfony\Component\DependencyInjection\ContainerInterface; /** @@ -36,6 +37,13 @@ class PlaceBlockPageVariant extends BlockPageVariant { protected $redirectDestination; /** + * The module handler. + * + * @var \Drupal\Core\Extension\ModuleHandlerInterface + */ + protected $moduleHandler; + + /** * Constructs a new PlaceBlockPageVariant. * * @param array $configuration @@ -54,12 +62,15 @@ class PlaceBlockPageVariant extends BlockPageVariant { * The theme manager. * @param \Drupal\Core\Routing\RedirectDestinationInterface $redirect_destination * The redirect destination. + * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler + * The module handler. */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, BlockRepositoryInterface $block_repository, EntityViewBuilderInterface $block_view_builder, array $block_list_cache_tags, ThemeManagerInterface $theme_manager, RedirectDestinationInterface $redirect_destination) { + public function __construct(array $configuration, $plugin_id, $plugin_definition, BlockRepositoryInterface $block_repository, EntityViewBuilderInterface $block_view_builder, array $block_list_cache_tags, ThemeManagerInterface $theme_manager, RedirectDestinationInterface $redirect_destination, ModuleHandlerInterface $module_handler) { parent::__construct($configuration, $plugin_id, $plugin_definition, $block_repository, $block_view_builder, $block_list_cache_tags); $this->themeManager = $theme_manager; $this->redirectDestination = $redirect_destination; + $this->moduleHandler = $module_handler; } /** @@ -74,7 +85,8 @@ public static function create(ContainerInterface $container, array $configuratio $container->get('entity_type.manager')->getViewBuilder('block'), $container->get('entity_type.manager')->getDefinition('block')->getListCacheTags(), $container->get('theme.manager'), - $container->get('redirect.destination') + $container->get('redirect.destination'), + $container->get('module_handler') ); } @@ -97,23 +109,43 @@ public function build() { 'region' => $region, ]; if ($destination) { - $query['destination'] = $destination; + $query['destination'] = $destination . "?region_sort=$region"; } $title = $this->t('Place block in the %region region', ['%region' => $region_name]); + // @todo Remove module exists check when offcanvas library moved into + // core.services.yml. https://www.drupal.org/node/2784443 + if ($this->moduleHandler->moduleExists('outside_in')) { + $place_block_route = 'block_place.admin_library'; + $data_dialog_attributes = [ + 'data-dialog-type' => 'dialog', + 'data-dialog-renderer' => 'offcanvas', + 'data-dialog-options' => Json::encode([ + 'width' => 350, + ]), + ]; + } + else { + $place_block_route = 'block.admin_library'; + $data_dialog_attributes = [ + 'data-dialog-type' => 'modal', + 'data-dialog-options' => Json::encode([ + 'width' => 700, + ]), + ]; + } + + $place_block_route = 'block_place.admin_library'; + $operations['block_description'] = [ '#type' => 'inline_template', '#template' => '
{{ link }}
', '#context' => [ - 'link' => Link::createFromRoute($title, 'block.admin_library', ['theme' => $theme_name], [ + 'link' => Link::createFromRoute($title, $place_block_route, ['theme' => $theme_name], [ 'query' => $query, 'attributes' => [ 'title' => $title, 'class' => ['use-ajax', 'button', 'button--small'], - 'data-dialog-type' => 'modal', - 'data-dialog-options' => Json::encode([ - 'width' => 700, - ]), - ], + ] + $data_dialog_attributes, ]), ], ]; diff --git a/core/modules/block_place/tests/src/Functional/BlockPlaceTest.php b/core/modules/block_place/tests/src/Functional/BlockPlaceTest.php index 8e89048..d7e965a 100644 --- a/core/modules/block_place/tests/src/Functional/BlockPlaceTest.php +++ b/core/modules/block_place/tests/src/Functional/BlockPlaceTest.php @@ -39,7 +39,7 @@ public function testPlacingBlocksAdmin() { $this->assertGreaterThan(0, count($visible_regions)); $default_theme = $this->config('system.theme')->get('default'); - $block_library_url = Url::fromRoute('block.admin_library', ['theme' => $default_theme]); + $block_library_url = Url::fromRoute('block_place.admin_library', ['theme' => $default_theme]); foreach ($visible_regions as $region => $name) { $block_library_url->setOption('query', ['region' => $region]); $links = $this->xpath('//a[contains(@href, :href)]', [':href' => $block_library_url->toString()]);