Hi - I am trying to add a separator between the items of the main menu. I have copied the links function into my own template.php and modified it as below.

It works, I am getting the " / " betwwen the menu items but I am receiving the message Notice: Undefined index: id in psl_links() (line 23 of /Library/WebServer/Documents/psl/dev/sites/all/themes/psl/template.php).

Line 23 is the if (($attributes['id']) == 'main-menu') { statement

Can anyone advise on what I am doing wrong please.

Thanks

function psl_links($vars) {
  global $language_url;
  
  $links = $vars['links'];
  $attributes = $vars['attributes'];
  $heading = $vars['heading'];
  
  $delimiter = '';
   
  if (($attributes['id']) == 'main-menu') {
  	$delimiter = '<span class="delimiter"> / </span>';
  };
  
  $output = '';

  if (count($links) > 0) {
    // Treat the heading first if it is present to prepend it to the list of links.
    if (!empty($heading)) {
      if (is_string($heading)) {
        // Prepare the array that will be used when the passed heading is a string.
        $heading = array(
          'text' => $heading,
          // Set the default level of the heading.
          'level' => 'h2',
        );
      }
      
      $output .= '<' . $heading['level'];
      if (!empty($heading['class'])) {
        $output .= drupal_attributes(array('class' => $heading['class']));
      }
      
      $output .= '>' . check_plain($heading['text']) . '</' . $heading['level'] . '>';
    }

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

    $num_links = count($links);
    $i = 1;

    foreach ($links as $key => $link) {
      $class = array($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_url->language)) {
        $class[] = 'active';
      }
      
      // Add a class for has a sub-menu
      if(isset($link['below'])) {
        $class[] = 'has-subitems';
      }
      
      $output .= '<li' . drupal_attributes(array('class' => $class)) . '>';

      if (isset($link['href'])) {
        // Pass in $link as $options, they share the same keys.
        $output .= l(trim($link['title']), $link['href'], $link);
      }
      elseif (!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>';
      }
      
      // Theme in nested links in the UL
      if(isset($link['below'])) {
        $output .= theme('links', array(
          'links' => $link['below'],
          'attributes' => array(
            'class' => array('links', 'main-menu', 'secondary-items'),
          ),
        ));
      }

  	if ($i == $num_links) {
	  $delimiter = '';
	  }

      $i++;
      $output .= "</li>" . $delimiter;
    }

    $output .= '</ul>';
  }

  return $output;
}

Comments

JSCSJSCS’s picture

Wow, that is a lot of code. I would have just changed the name of the menu items. Change "Menu Item" to "Menu Item / ", "Menu Item 2" to Menu Item 2 /", etc.

timtunbridge’s picture

You could although this would then become part of the anchor which I don't really want. The active class will turn the menu item a different colour which would include the forward slash using this suggestion - I don't want that to happen.

The above code works but there is something wrong with the statement

if (($attributes['id']) == 'main-menu') {
  	$delimiter = '<span class="delimiter"> / </span>';
  };

Can anyone advise or explain how I can work this out for myself (I'm relatively new to php and D7).

Thanks

timtunbridge’s picture

Issue summary: View changes

clarification