Hi, i'm currently developing my first Drupal website, so i'm very new to it. Now i'm trying to add a field to the menu item form which makes it possible to add a subtitle to a certain menu item (All menu items on the first level need a subtitle, and i can't add it to a node or so, because some first level items are created with views, so i thought this would be the best solution). I alread added the field to the menu item form using this code:

if ((isset($form['#node']) && $form['#node']->type .'_node_form' == $form_id) || ('menu_edit_item' == $form_id)) {

     if ($form['#node']->type .'_node_form' == $form_id) { // It's the node edit form
        $item = $form['#node']->menu;
      } else {
        $item = $form['menu']['#item'];
      }

      if (isset($form['menu'])) { // Check to see whether the menu form exists
        $form['menu']['options'] = array(
          '#type' => 'fieldset',
          '#title' => t('Menu item attributes'),
          '#collapsible' => FALSE,
          '#collapsed' => FALSE,
          '#tree' => TRUE,
          '#weight' => 50,
      );

        $form['menu']['options']['attributes']['subtitle'] = array(
          '#type' => 'textfield',
          '#title' => t('Menu link subtitle'),
          '#default_value' => isset($item['options']['attributes']['subtitle']) ? $item['options']['attributes']['subtitle'] : NULL,
          '#description' => t('The text used as subtitle for this menu item, this will be shown when active.'),
          '#required' => FALSE,
          '#weight' => 50,
        );
      }
    }

In hook_form_alter, saving and editing works. But now i need to show this subtitle in my template (page.tpl) and it seems that the current active menu item isn't available, so i tried hacking it:

<?php foreach($primary_links as $key => $menu_link): ?>
    	<?php if(strpos($key, 'active') && $menu_link["attributes"]["subtitle"] != ""): ?>
    		<h1 class="title"><?php print $menu_link["attributes"]["subtitle"]; ?></h1>
    	<?php endif ?>
<?php endforeach ?>

This code also works, but this isn't very performant and very clean, is there a better way to do this?

kind regards,
Daan

Comments

ivan_doric’s picture

I've been having the same problem, I used your code, but I've put it all in template.php file. I think that your code is actually a very good way to solve this problem. I just used the menu_form_alter function instead of hook_form_alter.

/


function THEME-NAME_links($links, $attributes = array('class' => 'links')) {

  global $language;

  $output = '';



  if (count($links) > 0) {

    $output = '<ul' . drupal_attributes($attributes) . '>';



    $num_links = count($links);

    $i = 1;



    foreach ($links as $key => $link) {

      $class = $key;



      // Add first, last and active classes to the list of links to help out themers.

      if ($i == 1) {

        $class .= ' first';

      }

      if ($i == $num_links) {

        $class .= ' last';

      }

      if (isset($link['href']) && ($link['href'] == $_GET['q'] || ($link['href'] == '<front>' && drupal_is_front_page()))

           && (empty($link['language']) || $link['language']->language == $language->language)) {

        $class .= ' active';

      }

      $output .= '<li' . drupal_attributes(array('class' => $class)) . '>';



      if (isset($link['href'])) {

        // Pass in $link as $options, they share the same keys.

        $output .= l($link['title'], $link['href'], $link);

        

      }

      else if (!empty($link['title'])) {

        // Some links are actually not links, but we wrap these in <span> for adding title and class attributes

        if (empty($link['html'])) {

          $link['title'] = check_plain($link['title']);

        }

        $span_attributes = '';

        if (isset($link['attributes'])) {

          $span_attributes = drupal_attributes($link['attributes']);

        }

        $output .= '<span' . $span_attributes . '>' . $link['title'] . '</span>';

      }

      
/*code that adds new <span> to the item */
      
if(isset($link['attributes']['subtitle'])){

      $output .= '<span>' . $link['attributes']['subtitle'] . '</span>';

      }


      $i++;

      $output .= "</li>\n";

    }



    $output .= '</ul><!--/end-->';

  }



  return $output;

}
ChristianP’s picture

nvm

RonanK’s picture

Hello,

Did you finally find the way to completely manage menu subtitles ? I'm also really interested and far to be good enough in development to do it myself...

albert volkman’s picture

Perhaps this could be useful- https://drupal.org/project/power_menu