1.20.x layout.module layout_get_layout_by_path($path = NULL, $router_item = NULL, $is_layout_page = FALSE)

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 placeholders represented by "%" symbols.

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

bool $is_layout_page: Flag that says whether this is being called from the page callback for a layout-provided page.

Return value

Layout: The Layout object for the specified path.

File

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

Code

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

  // Use the static cache, but keyed on the normal path (such as node/1), rather
  // than the system path (such as node/%), since client modules might serve up
  // different layouts for the same system path.
  $href = $router_item['href'];
  $layouts_by_path = &backdrop_static(__FUNCTION__, array());
  if (isset($layouts_by_path[$href])) {
    return $layouts_by_path[$href];
  }

  $layouts = layout_load_multiple_by_router_item($router_item, !$is_layout_page);
  $selected_layout = NULL;
  foreach ($layouts as $layout) {
    // Contexts must have their data set before the layout's access may be
    // checked.
    $contexts = $layout->getContexts();
    foreach ($contexts as $context) {
      if (isset($context->position)) {
        // If the menu router set the data for a context object or a client did
        // so via hook_layout_load_by_router_item_alter(), we don't need to do
        // any setting here. But if $context->data is not an object, then it is
        // either a raw string that must be converted to an object by a load
        // callback or it is a string pass-through.
        if (!is_object($context->data)) {
          // If clients replaced any layout with one whose placeholders are in a
          // new position, they need to have altered $context->position so that
          // it points to the right place in the router item to get the data
          // object.
          if (isset($router_item['map'][$context->position])) {
            $context_data = $router_item['map'][$context->position];
            // If the router item contains the context object in the right
            // position, then we can just set the data from that.
            if (is_object($context_data)) {
              $context->setData($context_data);
            }
            else {
              // If the context data was not an object, then we can try to set
              // it using the plugin's load callback. If there is no callback,
              // then it must be a string pass-through, so we leave it as is.
              $context_info = layout_get_context_info($context->plugin);
              if (isset($context_info['load callback'])) {
                $context_data = call_user_func_array($context_info['load callback'], array($router_item['original_map'][$context->position]));
              }
              $context->setData($context_data);
            }
          }
        }
      }
    }
    if (!$layout->disabled && $layout->checkAccess()) {
      $selected_layout = $layout;
      break;
    }
  }

  if (!$is_layout_page) {
    // If no layout matches at the path, use a 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');
      }
    }
  }

  $layouts_by_path[$href] = $selected_layout;
  return $selected_layout;
}