You are here

function groupmenu_node_prepare in Group Menu 7

Implements hook_node_prepare().

File

./groupmenu.module, line 325
Integrates menu with Group.

Code

function groupmenu_node_prepare($node) {
  if (!empty($node->nid) && !isset($node->group)) {
    return;
  }

  // New node.
  if (empty($node->nid)) {
    $node->groupmenu = (bool) variable_get('groupmenu_create_by_default', FALSE);
    $groups = empty($node->group) ? _groupmenu_get_user_group_ids() : array(
      $node->group,
    );
  }
  else {

    // Get all menus for available groups.
    $groups = array(
      $node->group,
    );
    $menus = groupmenu_get_group_menus($groups);
    $node->groupmenu = !empty($menus);
  }
  if (variable_get('groupmenu_enable_' . $node->type, FALSE)) {

    // Get all menus for available groups.
    $menus = groupmenu_get_group_menus($groups);

    // Store the menus for later use in form_alter and form_validate.
    $node->storage['groupmenu'] = $menus;

    // $node is not a new node and menu link is not set.
    if (!empty($node->nid) && empty($node->menu['link_title']) && !empty($menus)) {
      $menu_names = array();
      foreach ($menus as $menu) {
        $menu_names[] = $menu['menu_name'];
      }

      // This query comes from menu.modules node_prepare, and is how it does it.
      $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' => $menu_names,
      ))
        ->fetchField();
      if ($mlid) {

        // We've found something, so load the item and set that in the node
        // form.
        $item = menu_link_load($mlid);
        $options = menu_parent_options(array(
          $item['menu_name'],
        ), $item);
        if (!empty($options)) {
          $node->menu = $item;

          // 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);
          }
        }
      }
    }
  }
}