1 layout_renderer_standard.inc LayoutRendererStandard::prepareBlocks(array $blocks)

Prepare the list of blocks to be rendered, accounting for visibility/access settings and rendering order.

This method represents the standard approach for determining the list of blocks to be rendered that is compatible with all parts of the Layout architecture. It first applies visibility & access checks, then sort blocks into their proper rendering order, and returns the result as an array.

Inheriting classes should override this method if that renderer needs to regularly make additions to the set of blocks that will be rendered.

Parameters

array $blocks: An associative array of block data (Block objects), keyed by UUID.

Return value

array: An associative array of blocks to be rendered, keyed on UUID and sorted into proper rendering order.

File

core/modules/layout/plugins/renderers/layout_renderer_standard.inc, line 203

Class

LayoutRendererStandard
The standard render for a Layout display object.

Code

function prepareBlocks(array $blocks) {
  // Use local variables as writing to them is very slightly faster
  $first = $normal = $last = array();

  // Prepare the list of blocks to be rendered
  foreach ($blocks as $uuid => $block) {

    // Check access on each block.
    if (empty($this->admin)) {
      // Assign contexts to each block.
      $layout_contexts = $this->layout->getContexts();

      $has_contexts = TRUE;
      $required_contexts = $block->getRequiredContexts();
      if ($required_contexts) {
        $has_contexts = $this->layout->hasContexts($required_contexts);
        if ($has_contexts) {
          $block->setContexts($layout_contexts);
        }
      }
      elseif ($block->conditions) {
        $block->setContexts($layout_contexts);
      }

      // Then check access on the block.
      if (!$has_contexts || !$block->checkAccess()) {
        continue;
      }

      // Set the page title from the block.
      if ($this->layout->settings['title_display'] === LAYOUT_TITLE_BLOCK) {
        if ($block->uuid === $this->layout->settings['title_block']) {
          $this->page_title = $block->getTitle();
        }
      }
    }

    // Let the block prepare any data before rendering.
    $block->prepare();

    $block_info = layout_get_block_info($block->module, $block->delta);

    // If this block wants to render last, add it to the $last array. We allow
    // this because some blocks need to be rendered after other blocks,
    // primarily so they can do things like the leftovers of forms.
    if (!empty($block_info['render last'])) {
      $last[$uuid] = $block;
    }
    // If it wants to render first, add it to the $first array. This is used
    // by blocks that need to do some processing before other blocks are
    // rendered.
    elseif (!empty($block_info['render first'])) {
      $first[$uuid] = $block;
    }
    // Otherwise, render it in the normal order.
    else {
      $normal[$uuid] = $block;
    }
  }

  $this->prepared['blocks'] = $first + $normal + $last;
  return $this->prepared['blocks'];
}