You are here

function book_helper_init in Book helper 7

Implements hook_init().

File

./book_helper.module, line 34
Improves Drupal's core book module's functionality.

Code

function book_helper_init() {

  // Set main book page in menu tree path.
  if (variable_get('book_helper_menu_tree_set_path', 0) && function_exists('menu_tree_set_path')) {
    $node = menu_get_object();

    // Skip setting the menu tree path if we are on the main page of the book.
    if (isset($node->book) && $node->book['nid'] != $node->book['bid']) {

      // Get active menu names for this node.
      $active_menu_names = db_select('menu_links')
        ->distinct()
        ->fields('menu_links', array(
        'menu_name',
      ))
        ->condition('module', 'menu')
        ->condition('link_path', 'node/' . $node->nid)
        ->execute()
        ->fetchCol();

      // Only set path for core's 'menu' module.
      $menu_names = db_select('menu_links')
        ->distinct()
        ->fields('menu_links', array(
        'menu_name',
      ))
        ->condition('module', 'menu')
        ->orderBy('menu_name')
        ->execute()
        ->fetchCol();
      $path = 'node/' . $node->book['bid'];
      foreach ($menu_names as $menu_name) {

        // Make sure we don't reset the menu tree path if this node (aka page)
        // is already active in this menu.
        if (!in_array($menu_name, $active_menu_names)) {
          menu_tree_set_path($menu_name, $path);
        }
      }
    }
  }
}