1 menu.module menu_node_prepare(Node $node)

Implements hook_node_prepare().

File

core/modules/menu/menu.module, line 629
Allows administrators to customize the site's menus.

Code

function menu_node_prepare(Node $node) {
  if (empty($node->menu)) {
    $node_type = node_type_load($node->type);

    // Prepare the node for the edit form so that $node->menu always exists.
    $menu_name = strtok($node_type->settings['menu_parent'], ':');
    $item = array();
    if (isset($node->nid)) {
      $mlid = FALSE;
      // Give priority to the default menu
      $type_menus = $node_type->settings['menu_options'];
      if (in_array($menu_name, $type_menus)) {
        $mlid = db_query_range("SELECT mlid FROM {menu_links} WHERE link_path = :path AND menu_name = :menu_name AND module = 'menu' ORDER BY mlid ASC", 0, 1, array(
          ':path' => 'node/' . $node->nid,
          ':menu_name' => $menu_name,
        ))->fetchField();
      }
      // Check all allowed menus if a link does not exist in the default menu.
      if (!$mlid && !empty($type_menus)) {
        $mlid = db_query_range("SELECT mlid FROM {menu_links} WHERE link_path = :path AND module = 'menu' AND menu_name IN (:type_menus) ORDER BY mlid ASC", 0, 1, array(
          ':path' => 'node/' . $node->nid,
          ':type_menus' => array_values($type_menus),
        ))->fetchField();
      }
      if ($mlid) {
        $item = menu_link_load($mlid);
      }
    }
    // Set the dummy item langcode to the same language as the node.
    if (module_exists('language') && empty($item)) {
      $item['langcode'] = $node->langcode;
    }
    // Set default values.
    $node->menu = $item + array(
      'link_title' => '',
      'mlid' => 0,
      'plid' => 0,
      'menu_name' => $menu_name,
      'weight' => 0,
      'options' => array(),
      'module' => 'menu',
      'expanded' => 0,
      'hidden' => 0,
      'has_children' => 0,
      'customized' => 0,
    );
  }
  // Find the depth limit for the parent select.
  if (!isset($node->menu['parent_depth_limit'])) {
    $node->menu['parent_depth_limit'] = _menu_parent_depth_limit($node->menu);
  }
}