blocksInRegions === NULL) { $this->sortBlocks(); } if (!isset($this->blocksInRegions[$region])) { throw new \Exception(sprintf("Region %region does not exist in layout %layout", array('%region' => $region, '%layout' => $this->getLayoutInstance()->name)), E_RECOVERABLE_ERROR); } return $this->blocksInRegions[$region]; } /** * Implements BoundDisplayInterface::getAllSortedBlocks(). */ public function getAllSortedBlocks() { if ($this->blocksInRegions === NULL) { $this->sortBlocks(); } return $this->blocksInRegions; } /** * Transform the stored blockConfig into a sorted, region-oriented array. */ protected function sortBlocks() { $layout_instance = $this->getLayoutInstance(); if ($this->layout !== $layout_instance->getPluginId()) { $block_config = $this->mapBlocksToLayout($layout_instance); } else { $block_config = $this->blockInfo; } $this->blocksInRegions = array(); $regions = array_fill_keys(array_keys($layout_instance->getRegions()), array()); foreach ($block_config as $config_name => $info) { $regions[$info['region']][$config_name] = $info; } foreach ($regions as $region_name => &$blocks) { uasort($blocks, 'drupal_sort_weight'); $this->blocksInRegions[$region_name] = array_keys($blocks); } } /** * Implements BoundDisplayInterface::remapToLayout(). */ public function remapToLayout(LayoutInterface $layout) { $this->blockInfo = $this->mapBlocksToLayout($layout); $this->setLayout($layout->getPluginId()); } /** * Set the contained layout plugin. * * @param string $plugin_id * The plugin id of the desired layout plugin. */ public function setLayout($plugin_id) { // @todo verification? $this->layout = $plugin_id; $this->layoutInstance = NULL; $this->blocksInRegions = NULL; } /** * Implements BoundDisplayInterface::generateUnboundDisplay(). * * @throws \Exception */ public function generateUnboundDisplay($id, $entity_type = 'unbound_display') { $block_info = $this->getAllBlockInfo(); foreach ($block_info as &$info) { unset($info['region']); } $values = array( 'blockInfo' => $block_info, 'id' => $id, ); $entity = entity_create($entity_type, $values); if (!$entity instanceof UnboundDisplayInterface) { throw new \Exception(sprintf('Attempted to create an unbound display using an invalid entity type.'), E_RECOVERABLE_ERROR); } return $entity; } /** * Returns the instantiated layout object. * * @throws \Exception */ public function getLayoutInstance() { if ($this->layoutInstance === NULL) { if (empty($this->layout)) { throw new \Exception(sprintf('Display "%id" had no layout plugin attached.', array('%id' => $this->id())), E_RECOVERABLE_ERROR); } $this->layoutInstance = layout_manager()->createInstance($this->layout, $this->layoutSettings); // @todo add handling for remapping if the layout could not be found } return $this->layoutInstance; } }