1

In my module, I need to create sort of a menu block consisting of certain menu items collected from various menus, and flattened into one level.

I'm not sure about the correct chain of the Menu API functions to call. My first attempt was as follows:

function _MYMODULE_block_view(){
  $html = '';

  $menu_link = menu_link_get_preferred('node/287');
  $html .= theme('menu_link', $menu_link);
  dpm($menu_link);
  return $html;
}

but it raises an error about several #attributes missing, so I guess this is not the right kind of object to get markup from. The API docs don't really help in understanding what kind of objects/parameters are returned/expected by functions, hence the question.

Edit:

Having given what Drupal complained about, the code now is as follows:

function _MYMODULE_block_view(){
  global $language;
  $langcode = $language->language;

  $html = '';

  $paths = array(
    'node/287',
    'node/3',
  );

  foreach($paths as $path){
    $translations = i18n_get_path_translations($path);
    if(!empty($translations[$langcode])){
      $i18n_path = $translations[$langcode]['href'];

      $menu_link = menu_link_get_preferred($i18n_path);
      $link = array(
        '#theme' => 'link',
        '#text' => $menu_link['link_title'],
        '#path' => $path,
        '#options' => array('attributes' => array('title' => $menu_link['link_title'])),
        '#below' => NULL,
        '#title' => $menu_link['link_title'],
        '#href' => $path,
        '#attributes' => array(),
        '#localized_options' => array(),
        '#prefix' => '<li>',
        '#suffix' => '</li>'
      );
      $html .= theme('menu_link', array('element' => $link));
    }
  }

  return "<ul>$html</ul>";
}

It does not spit out errors but... should I really trust it?

0

Since you're using menu_link_get_preferred() I'm guessing Drupal 7 here...

Theme functions have changed, and parameters now need to be named in an associative array passed to the theme function. So per the theme_menu_link() docs:

$html .= theme('menu_link', $menu_link);

becomes

$html .= theme('menu_link', array('element' => $menu_link));
4
  • Thanks, good to know, but this code gives me error as previously. Please see my edited question with updated code.
    – Artur
    Feb 7 '13 at 13:06
  • Next edit: error-free, but is it Drupal-way?
    – Artur
    Feb 7 '13 at 13:34
  • Nothing wrong with that as far as I can tell...you could use theme_item_list() to avoid having to prefix and suffix the element with list tags, but that's a small improvement
    – Clive
    Feb 7 '13 at 13:36
  • 1
    I don't like theme_item_list() for it has hardcoded wrapper and class :) but that's another story. Thanks Clive for pointing me in the right direction.
    – Artur
    Feb 7 '13 at 13:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.