1 node.module node_layout_load_by_router_item_alter(&$layouts, $router_item)

Implements hook_layout_load_by_router_item_alter().

When creating node preview pages, the path will be of the form node/preview/<type>/<id>, where <type> is the node type and <id> is the tempstore ID of the node begin previewed. But we want to display it using a layout whose system path is node/%. So we choose those layouts and set the context from the tempstore node.

File

core/modules/node/node.module, line 1669
The core module that allows content to be submitted to the site.

Code

function node_layout_load_by_router_item_alter(&$layouts, $router_item) {
  $map = $router_item['map'];
  if (count($map) != 4 || $map[0] != 'node' || $map[1] != 'preview') {
    return;
  }
  $path_is_preview = FALSE;
  foreach (node_type_get_types() as $type_obj) {
    if ($type_obj->type == $map[2]) {
      $path_is_preview = TRUE;
      break;
    }
  }
  if (!$path_is_preview) {
    return;
  }
  $tempstore_id = $map[3];
  $temp_node = node_get_node_tempstore($tempstore_id);
  if ($temp_node) {
    $node_layouts = layout_load_multiple_by_path('node/%', TRUE);
    if ($node_layouts) {
      $layouts = $node_layouts;
      foreach ($layouts as $layout) {
        foreach ($layout->getContexts() as $context) {
          if (isset($context->position)) {
            $context->setData($temp_node);
          }
        }
      }
    }
  }
}