From 06343effacd9bd702976fb01e1458ede61cf9020 Mon Sep 17 00:00:00 2001
From: webchick <drupal@webchick.net>
Date: Fri, 26 Jan 2018 12:50:00 -0800
Subject: [PATCH] Issue #2937159 by tim.plunkett, Kristen Pol, EclipseGc,
 drpal: Manipulating placement of components in Layout Builder UI is
 inconsistent

---
 .../layout_builder/js/layout-builder.es6.js      | 16 ++++++++++------
 core/modules/layout_builder/js/layout-builder.js |  8 ++++++--
 .../layout_builder/layout_builder.routing.yml    |  2 +-
 .../src/Controller/LayoutBuilderController.php   |  2 +-
 .../src/Controller/MoveBlockController.php       |  6 ++----
 5 files changed, 20 insertions(+), 14 deletions(-)

diff --git a/core/modules/layout_builder/js/layout-builder.es6.js b/core/modules/layout_builder/js/layout-builder.es6.js
index 391e462cc677..cdb5bee76299 100644
--- a/core/modules/layout_builder/js/layout-builder.es6.js
+++ b/core/modules/layout_builder/js/layout-builder.es6.js
@@ -14,15 +14,19 @@
          *   An object containing information about the item being sorted.
          */
         update(event, ui) {
-          // Only process if the item was moved from one region to another.
-          if (ui.sender) {
+          // Check if the region from the event and region for the item match.
+          const itemRegion = ui.item.closest('.layout-builder--layout__region');
+          if (event.target === itemRegion[0]) {
+            // Find the destination delta.
+            const deltaTo = ui.item.closest('[data-layout-delta]').data('layout-delta');
+            // If the block didn't leave the original delta use the destination.
+            const deltaFrom = ui.sender ? ui.sender.closest('[data-layout-delta]').data('layout-delta') : deltaTo;
             ajax({
               url: [
                 ui.item.closest('[data-layout-update-url]').data('layout-update-url'),
-                ui.sender.closest('[data-layout-delta]').data('layout-delta'),
-                ui.item.closest('[data-layout-delta]').data('layout-delta'),
-                ui.sender.data('region'),
-                $(this).data('region'),
+                deltaFrom,
+                deltaTo,
+                itemRegion.data('region'),
                 ui.item.data('layout-block-uuid'),
                 ui.item.prev('[data-layout-block-uuid]').data('layout-block-uuid'),
               ]
diff --git a/core/modules/layout_builder/js/layout-builder.js b/core/modules/layout_builder/js/layout-builder.js
index d4dadc89344c..661c5e3d841b 100644
--- a/core/modules/layout_builder/js/layout-builder.js
+++ b/core/modules/layout_builder/js/layout-builder.js
@@ -16,9 +16,13 @@
         connectWith: '.layout-builder--layout__region',
 
         update: function update(event, ui) {
-          if (ui.sender) {
+          var itemRegion = ui.item.closest('.layout-builder--layout__region');
+          if (event.target === itemRegion[0]) {
+            var deltaTo = ui.item.closest('[data-layout-delta]').data('layout-delta');
+
+            var deltaFrom = ui.sender ? ui.sender.closest('[data-layout-delta]').data('layout-delta') : deltaTo;
             ajax({
-              url: [ui.item.closest('[data-layout-update-url]').data('layout-update-url'), ui.sender.closest('[data-layout-delta]').data('layout-delta'), ui.item.closest('[data-layout-delta]').data('layout-delta'), ui.sender.data('region'), $(this).data('region'), ui.item.data('layout-block-uuid'), ui.item.prev('[data-layout-block-uuid]').data('layout-block-uuid')].filter(function (element) {
+              url: [ui.item.closest('[data-layout-update-url]').data('layout-update-url'), deltaFrom, deltaTo, itemRegion.data('region'), ui.item.data('layout-block-uuid'), ui.item.prev('[data-layout-block-uuid]').data('layout-block-uuid')].filter(function (element) {
                 return element !== undefined;
               }).join('/')
             }).execute();
diff --git a/core/modules/layout_builder/layout_builder.routing.yml b/core/modules/layout_builder/layout_builder.routing.yml
index 085eb301114b..98465538625e 100644
--- a/core/modules/layout_builder/layout_builder.routing.yml
+++ b/core/modules/layout_builder/layout_builder.routing.yml
@@ -99,7 +99,7 @@ layout_builder.remove_block:
         layout_builder_tempstore: TRUE
 
 layout_builder.move_block:
-  path: '/layout_builder/move/block/{section_storage_type}/{section_storage}/{delta_from}/{delta_to}/{region_from}/{region_to}/{block_uuid}/{preceding_block_uuid}'
+  path: '/layout_builder/move/block/{section_storage_type}/{section_storage}/{delta_from}/{delta_to}/{region_to}/{block_uuid}/{preceding_block_uuid}'
   defaults:
     _controller: '\Drupal\layout_builder\Controller\MoveBlockController::build'
     delta_from: null
diff --git a/core/modules/layout_builder/src/Controller/LayoutBuilderController.php b/core/modules/layout_builder/src/Controller/LayoutBuilderController.php
index 3f3b95e49b21..5ccddfac38aa 100644
--- a/core/modules/layout_builder/src/Controller/LayoutBuilderController.php
+++ b/core/modules/layout_builder/src/Controller/LayoutBuilderController.php
@@ -215,7 +215,7 @@ protected function buildAdministrativeSection(SectionStorageInterface $section_s
       ];
       $build[$region]['layout_builder_add_block']['#type'] = 'container';
       $build[$region]['layout_builder_add_block']['#attributes'] = ['class' => ['add-block']];
-      $build[$region]['layout_builder_add_block']['#weight'] = -1000;
+      $build[$region]['layout_builder_add_block']['#weight'] = 1000;
       $build[$region]['#attributes']['data-region'] = $region;
       $build[$region]['#attributes']['class'][] = 'layout-builder--layout__region';
     }
diff --git a/core/modules/layout_builder/src/Controller/MoveBlockController.php b/core/modules/layout_builder/src/Controller/MoveBlockController.php
index 7411aa0bbe3b..cb8e71f4b7e7 100644
--- a/core/modules/layout_builder/src/Controller/MoveBlockController.php
+++ b/core/modules/layout_builder/src/Controller/MoveBlockController.php
@@ -56,8 +56,6 @@ public static function create(ContainerInterface $container) {
    *   The delta of the original section.
    * @param int $delta_to
    *   The delta of the destination section.
-   * @param string $region_from
-   *   The original region for this block.
    * @param string $region_to
    *   The new region for this block.
    * @param string $block_uuid
@@ -68,7 +66,7 @@ public static function create(ContainerInterface $container) {
    * @return \Drupal\Core\Ajax\AjaxResponse
    *   An AJAX response.
    */
-  public function build(SectionStorageInterface $section_storage, $delta_from, $delta_to, $region_from, $region_to, $block_uuid, $preceding_block_uuid = NULL) {
+  public function build(SectionStorageInterface $section_storage, $delta_from, $delta_to, $region_to, $block_uuid, $preceding_block_uuid = NULL) {
     $section = $section_storage->getSection($delta_from);
 
     $component = $section->getComponent($block_uuid);
@@ -87,7 +85,7 @@ public function build(SectionStorageInterface $section_storage, $delta_from, $de
       $section->insertAfterComponent($preceding_block_uuid, $component);
     }
     else {
-      $section->appendComponent($component);
+      $section->insertComponent(0, $component);
     }
 
     $this->layoutTempstoreRepository->set($section_storage);
-- 
GitLab