views:

8

answers:

0

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

related questions