diff --git a/core/modules/layout/layouts/static/one-col/one-col.tpl.php b/core/modules/layout/layouts/static/one-col/one-col.tpl.php index 61af832..529eb06 100644 --- a/core/modules/layout/layouts/static/one-col/one-col.tpl.php +++ b/core/modules/layout/layouts/static/one-col/one-col.tpl.php @@ -12,7 +12,7 @@ */ ?>
> -
+
diff --git a/core/modules/layout/layouts/static/twocol/two-col.tpl.php b/core/modules/layout/layouts/static/twocol/two-col.tpl.php index 810a052..ab59cac 100644 --- a/core/modules/layout/layouts/static/twocol/two-col.tpl.php +++ b/core/modules/layout/layouts/static/twocol/two-col.tpl.php @@ -14,11 +14,11 @@ */ ?>
> -
+
-
+
diff --git a/core/modules/display/display.admin.js b/core/modules/display/display.admin.js index b6deb9b..aea15c1 100644 --- a/core/modules/display/display.admin.js +++ b/core/modules/display/display.admin.js @@ -7,6 +7,30 @@ */ Drupal.behaviors.displayEditor = { attach: function (context) { + + function serializeBlocks() { + var regionBlocks = {}; + $('.layout-region', context).each(function() { + var pattern = new RegExp('layout-region-.+'); + var classes = $(this).attr('class').split(' '); + + // Look for the class with the region name. + for (var i = 0; i < classes.length; i++) { + if (pattern.test(classes[i])) { + // Store the block order for this region. + var regionName = classes[i].replace('layout-region-', ''); + regionBlocks[regionName] = []; + var blocks = $(this).find('.region-blocks').sortable('toArray'); + for (var j = 0; j < blocks.length; j++) { + regionBlocks[regionName].push(blocks[j].replace('block-', '')); + }; + break; + } + }; + }); + $('#edit-regions', context).val(JSON.stringify(regionBlocks)); + } + // Attach click handler to add block button. $('.region-table .add-block', context).once('display-add-block', function() { $(this).click(function () { @@ -20,8 +44,10 @@ Drupal.behaviors.displayEditor = { } // Add HTML code for block demonstration. - var block = '
M' + randomString + ' block
'; + var block = '
M' + randomString + ' block
'; $(this).parent().prepend(block); + + serializeBlocks(); }); }); @@ -31,6 +57,7 @@ Drupal.behaviors.displayEditor = { items: '.block', connectWith: '.connected-sortable', cursor: 'move', + update: serializeBlocks, }); $(this).disableSelection(); }); diff --git a/core/modules/display/lib/Drupal/display/DisplayFormController.php b/core/modules/display/lib/Drupal/display/DisplayFormController.php index c81be0b..78598bd 100644 --- a/core/modules/display/lib/Drupal/display/DisplayFormController.php +++ b/core/modules/display/lib/Drupal/display/DisplayFormController.php @@ -32,8 +32,10 @@ public function form(array $form, array &$form_state, EntityInterface $display) if (!isset($form_state['values'])) { $form_state['values'] = array(); } + $regions = $display->get('blockInfo'); $form_state['values'] += array( 'layout' => isset($display->layout) ? $display->layout : reset($layout_keys), + 'regions' => empty($regions) ? array() : $regions, ); $form['layout'] = array( @@ -58,7 +60,11 @@ public function form(array $form, array &$form_state, EntityInterface $display) $form['blocks'] = array( '#prefix' => '
', '#suffix' => '
', - 'demonstration' => $this->layoutDemonstration($display), + ); + $form['blocks']['demonstration'] = $this->layoutDemonstration($display); + $form['blocks']['regions'] = array( + '#type' => 'textarea', + '#default_value' => drupal_json_encode($form_state['values']['regions']), ); return parent::form($form, $form_state, $display); @@ -73,6 +79,9 @@ private function layoutDemonstration(EntityInterface $display) { $layout = layout_manager()->createInstance($display->layout, array()); $regions = $layout->getRegions(); foreach ($regions as $region => $info) { + // @todo Generate block demonstration HTML based on block HTML pattern + // in JS. + $existing_blocks = $display->getSortedBlocksByRegion($region); $region_text = '
' . check_plain($info['label']) . '
'; $region_text .= '
'; $region_text .= '
'; @@ -111,7 +120,31 @@ protected function actions(array $form, array &$form_state) { * Overrides Drupal\Core\Entity\EntityFormController::save(). */ public function save(array $form, array &$form_state) { + + // Create the block information array needed for the display object. + $blockInfo = array(); + $new_blocks = drupal_json_decode($form_state['values']['regions']); + + // Region type information is derived from the selected layout. + $layout_regions = layout_manager()->createInstance($form_state['values']['layout'], array())->getRegions(); + + foreach ($new_blocks as $region => $blocks) { + $weight = 0; + foreach ($blocks as $block) { + $blockInfo['block.' . $block] = array( + 'region' => $region, + // Increase weights by 100 so they are far enough apart for + // page block placement. + 'weight' => ($weight += 100), + 'region-type' => $layout_regions[$region]['type'], + ); + } + } + unset($form_state['values']['regions']); + + // Build and save the new display with the new block information. $display = $this->getEntity($form_state); + $display->set('blockInfo', $blockInfo); $display->save(); watchdog('display', 'Layout @label saved.', array('@label' => $display->label()), WATCHDOG_NOTICE);