1 layout.class.inc Layout::setBlockPosition($block_uuid, $region_name, $position = NULL)

Move an existing block from its current region to a new one.

Parameters

string $block_uuid: The UUID of the block whose position is being retrieved.

string $region_name: The name of the region to which the block will be moved.

File

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

Class

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

Code

function setBlockPosition($block_uuid, $region_name, $position = NULL) {
  // Set to the last position if position is greater than available places.
  if (empty($this->positions[$region_name]) || $position > count($this->positions[$region_name])) {
    $position = NULL;
  }

  // Find the current region.
  if ($current_region = $this->getBlockPosition($block_uuid)) {
    $current_position = array_search($block_uuid, $this->positions[$current_region]);
    // Remove the block from the current region.
    if ($current_position !== FALSE) {
      unset($this->positions[$current_region][$current_position]);
      $this->positions[$current_region] = array_values($this->positions[$current_region]);
    }
  }

  // Add the block to the new region in the correct position.
  if (isset($region_position)) {
    $new_positions = array();
    foreach (array_values($this->positions[$region_name]) as $region_position => $existing_uuid) {
      if ($region_position === $position) {
        $new_positions[] = $block_uuid;
      }
      $new_positions[] = $existing_uuid;
    }
    $this->positions[$region_name] = $new_positions;
  }
  else {
    $this->positions[$region_name][] = $block_uuid;
  }
}