1.20.x layout.class.inc Layout::setLayoutTemplate($layout_name, $region_mapping = array())

Set the layout template, accommodating moving blocks if necessary.

Parameters

$layout_name: The new layout name.

$region_mapping: An array of region names, each keyed by the old region name and the value of the new region name. If empty, all known regions will be copied from one to the other. To discard the contents of a region, set the value to FALSE.

File

modules/layout/includes/layout.class.inc, line 704
Class for loading, modifying, and executing a layout.

Class

Layout
@file Class for loading, modifying, and executing a layout.

Code

function setLayoutTemplate($layout_name, $region_mapping = array()) {
  // If not changing anything, return as a no-op.
  if ($layout_name === $this->layout_template && empty($region_mapping)) {
    return;
  }
  // If no layout has yet been set, then we have no copying to do.
  if (!isset($this->layout_template)) {
    $this->layout_template = $layout_name;
    return;
  }

  $old_layout_info = layout_get_layout_template_info($this->layout_template);
  $new_layout_info = layout_get_layout_template_info($layout_name);
  // Set up the array with region names for the new layout.
  $new_positions = array();
  $new_regions = array_keys($new_layout_info['regions']);
  foreach ($new_regions as $new_region_name) {
    $new_positions[$new_region_name] = array();
  }

  // Do the mapping in the order provided (if any).
  if ($region_mapping) {
    $regions = array_keys($region_mapping);
    $regions = array_unique(array_merge($regions, array_keys($old_layout_info['regions'])));
  }
  else {
    $regions = array_keys($old_layout_info['regions']);
  }
  foreach ($regions as $old_region_name) {
    if (isset($region_mapping[$old_region_name])) {
      $new_region_name = $region_mapping[$old_region_name];
    }
    elseif (array_key_exists($old_region_name, $new_layout_info['regions'])) {
      $new_region_name = $old_region_name;
    }
    else {
      $new_region_name = '';
    }

    if (isset($this->positions[$old_region_name])) {
      foreach ($this->positions[$old_region_name] as $uuid) {
        if (!empty($new_region_name)) {
          $new_positions[$new_region_name][] = $uuid;
        }
        elseif ($new_region_name === '') {
          $this->orphaned_blocks[] = $uuid;
        }
        else {
          unset($this->content[$uuid]);
        }
      }
    }
  }

  // Assign orphan blocks to the default or last region, if the former doesn't
  // exist.
  if (!empty($this->orphaned_blocks)) {
    end($new_positions);
    $this->refuge_region = !empty($new_layout_info['default region']) ? $new_layout_info['default region'] : key($new_positions);
    foreach ($this->orphaned_blocks as $uuid) {
      $new_positions[$this->refuge_region][] = $uuid;
    }
  }

  $this->layout_template = $layout_name;
  $this->positions = $new_positions;
  module_invoke_all('layout_template_change', $this, $old_layout_info['name']);
}