You are here

menu_firstchild.module in Menu Firstchild 7

menu_firstchild.module Main file for the menu_firstchild module.

By default, Drupal 6 requires that you enter a path for each menu link you add/edit from the Menu administration page. There are cases you may want to create a parent item, without any path, that simply links to its first viewable child item. Menu Firstchild provides this functionality. Developped by Henri MEDOT <henri.medot[AT]absyx[DOT]fr> http://www.absyx.fr

File

menu_firstchild.module
View source
<?php

/**
 * @file menu_firstchild.module
 * Main file for the menu_firstchild module.
 *
 * By default, Drupal 6 requires that you enter a path for each menu link you add/edit from the Menu administration page.
 * There are cases you may want to create a parent item, without any path, that simply links to its first viewable child item.
 * Menu Firstchild provides this functionality.
 * Developped by Henri MEDOT <henri.medot[AT]absyx[DOT]fr>
 * http://www.absyx.fr
 */

/**
 * Implementation of hook_menu().
 */
function menu_firstchild_menu() {
  $items['<firstchild>'] = array(
    'page callback' => '_menu_firstchild_menu',
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
  );
  return $items;
}
function _menu_firstchild_menu() {
  return drupal_not_found();
}

/**
 * Implementation of hook_menu_link_alter().
 */
function menu_firstchild_menu_link_alter(&$item) {
  if ($item['module'] == 'menu') {
    if ($item['link_path'] == '<firstchild>') {
      $item['options']['alter'] = TRUE;
      $item['options']['unaltered_hidden'] = $item['hidden'];
    }
  }
}

/**
 * Implementation of hook_translated_menu_link_alter().
 */
function menu_firstchild_translated_menu_link_alter(&$item, $map) {
  if ($item['module'] == 'menu' && $item['link_path'] == '<firstchild>') {
    $href = _menu_firstchild_get_firstchild_href($item['mlid']);
    if ($href != NULL) {
      $item['href'] = $href;
      $item['localized_options']['attributes']['class'][] = 'menu-firstchild';
    }
    else {
      $item['hidden'] = TRUE;
    }
  }
}
function _menu_firstchild_get_firstchild_href($mlid) {
  global $language;
  $result = db_query('SELECT mlid, link_path FROM {menu_links} WHERE plid = :plid ORDER BY weight, link_title', array(
    ':plid' => $mlid,
  ));
  foreach ($result as $m) {
    if ($m->link_path != '<firstchild>') {
      $child = menu_link_load($m->mlid);
      if (!empty($child['access']) && empty($child['hidden']) && (!isset($child['localized_options']['langcode']) || $child['localized_options']['langcode'] == $language->language)) {
        return $m->link_path;
      }
    }
    else {
      $href = _menu_firstchild_get_firstchild_href($m->mlid);
      if ($href != NULL) {
        return $href;
      }
    }
  }
  return NULL;
}

/**
 * Implementation of hook_form_alter().
 */
function menu_firstchild_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'menu_overview_form') {
    foreach ($form as $key => $value) {
      if (isset($value['#item']['href']) && $value['#item']['href'] == '<firstchild>') {
        $item = $value['#item'];
        $unaltered_hidden = isset($item['options']['unaltered_hidden']) ? $item['options']['unaltered_hidden'] : FALSE;
        $form[$key]['#item']['hidden'] = $unaltered_hidden;
        $form[$key]['hidden']['#default_value'] = !$unaltered_hidden;
        $form[$key]['#attributes']['class'] = $unaltered_hidden ? array(
          'menu-disabled',
        ) : array(
          'menu-enabled',
        );
        $form[$key]['title']['#markup'] = check_plain($item['title']) . ($unaltered_hidden ? ' (' . t('disabled') . ')' : '');
      }
    }
  }
  elseif ($form_id == 'menu_edit_item' && isset($form['link_path']['#description'])) {
    $form['link_path']['#description'] .= t(' Enter %firstchild to link to the item\'s first accessible child.', array(
      '%firstchild' => '<firstchild>',
    ));
  }
}

/**
 * Implementation of hook_tokens_alter().
 */
function menu_firstchild_tokens_alter(array &$replacements, array $context) {
  if (isset($replacements['[node:menu-link:parent:url:path]'])) {
    if (isset($context['data']['menu-link']) && $context['data']['menu-link']['link_path'] === '<firstchild>') {
      $parent = _menu_firstchild_find_non_firstchild_parent($context['data']['menu-link']);
      $replacements['[node:menu-link:parent:url:path]'] = drupal_get_path_alias($parent['link_path']);
    }
    if ($replacements['[node:menu-link:parent:url:path]'] === '<firstchild>') {
      $replacements['[node:menu-link:parent:url:path]'] = '';
    }
  }
}
function _menu_firstchild_find_non_firstchild_parent(array $menu_link) {
  if ($menu_link['link_path'] !== '<firstchild>') {
    return $menu_link;
  }
  $parent = menu_link_load($menu_link['plid']);
  if (FALSE === $parent) {
    return $menu_link;
  }
  if ($parent['link_path'] === '<firstchild>') {
    $parent = _menu_firstchild_find_non_firstchild_parent($parent);
  }
  return $parent;
}