1 layout.module layout_get_layout_by_path($path = NULL, $router_item = NULL)

Get the layout which is active based on a path or router item.

This may also be used to get the current layout on a page if no parameters are passed. In which case the current path will be used.

@since 1.4.0

Parameters

string $path: The menu routing path, with all wildcards represented by "%" symbols.

$router_item: The menu router item for the page currently being loaded. The $path parameter will be ignored if $router_item is specified.

Return value

Layout: The Layout object for the specified path.

File

core/modules/layout/layout.module, line 1197
The Layout module creates pages and wraps existing pages in layouts.

Code

function layout_get_layout_by_path($path = NULL, $router_item = NULL) {
  if (!isset($router_item)) {
    $router_item = menu_get_item($path);
  }

  $cache = &backdrop_static(__FUNCTION__);

  if (empty($cache[$router_item['path']])) {
    // If the path is a node preview path, check if a custom layout exists for
    // nodes and use that instead.
    // @ todo: This adds very specific code for node previews which too closely
    // couples Node and Layout modules. This needs to be reverted in a future
    // commit.
    $data = NULL;
    $node_types = node_type_get_types();
    foreach ($node_types as $type) {
      $preview_path = 'node/' . $type->type . '/preview';
      if ($router_item['path'] == $preview_path) {
        $tempstore_id = node_build_tempstore_id($type->type);
        $data = node_get_node_tempstore($tempstore_id);
        break;
      }
    }
    if ($data && $node_layouts = layout_load_multiple_by_path('node/%', TRUE)) {
      $layouts = $node_layouts;
    }
    else {
      $layouts = layout_load_multiple_by_path($router_item['path'], TRUE);
    }
    $selected_layout = NULL;
    foreach ($layouts as $layout) {
      // Contexts must be set before the layout's access may be checked.
      $contexts = $layout->getContexts();
      foreach ($contexts as $context) {
        if (isset($context->position)) {

          // Check for an object loaded by the menu router. Use a preview from
          // Tempstore if on a node preview page.
          // @ todo: This is also specific code for node previews which needs
          // to be reverted in a future commit.
          if (isset($router_item['map'][$context->position])) {
            $data = isset($data) ? $data : $router_item['map'][$context->position];
            $context->setData($data);
          }

          // If no context is set, load one using the layout info.
          if (!is_object($context->data)) {
            $context_info = layout_get_context_info($context->plugin);
            if (isset($context_info['load callback'])) {
              $function = $context_info['load callback'];
              $context_data = $function($router_item['original_map'][$context->position]);
              $context->setData($context_data);
            }
          }
        }
      }

      if (!$layout->disabled && $layout->checkAccess()) {
        $selected_layout = $layout;
        break;
      }
    }

    // If no layout matches at the path, use the default layout.
    if (!$selected_layout) {
      if (path_is_admin($router_item['path']) && user_access('view the administration theme')) {
        $selected_layout = layout_load('admin_default');
      }
      else {
        $selected_layout = layout_load('default');
      }
    }

    $cache[$router_item['path']] = $selected_layout;
  }

  return $cache[$router_item['path']];
}