Community & Support

How do I pull a CCK field from a node associated with a menu-item into a menu block

I am using the menu-block module (http://drupal.org/project/menu_block)

to generate 2nd level and 3rd level navigation blocks for my site. Everything is based off of Drupal's primary links and it's children.

I need to grab a custom CCK field I defined in the node associated with a menu-item and display it in the menu-block

So for example there is a top level navigation item "About Us" with 3 children. "Team" "Careers" and "Contact Us"

Within the Drupal admin I can see the nodes associated with each menu-item. Each one of these nodes has a CCK field called "menu_teaser"

I need to write out the "menu_teaser" field under the link in the menu-block generated.

Any help would be appreciated, let me know if I am posting this in the right place or if it should be module development

Thanks!

Comments

Figured it out

I ended up figuring this out myself and decided to put it up on here in case anyone else needs it.

I ended up starting by using the nice menus module instead of menu blocks.

Then I created a menu_teaser field for the content types that will be menu items.

I copied over the theme_nice_menu_build from the module to my template.php. Then just added in a function to grab the field with the nodeID if it exists.

Here is my template.php file

<?php
/**
* Returns menu_teaser field of a node associated with a menu item.
*
* @param $node_path
*   The path to the node, generated below in collective_nice_menu_build
*/
function c_menu_teaser($node_path) {
 
$thenode = node_load($node_path);
  if (!empty(
$thenode->field_menu_teaser[0][value])) {
     
$menu_teaser = $thenode->field_menu_teaser[0][value];
      return
$menu_teaser;
  }
}

function
yourtemplatename_nice_menu_build($menu) {
 
$output = '';

  foreach (
$menu as $menu_item) {
   
$mlid = $menu_item['link']['mlid'];
   
// Check to see if it is a visible menu item.
   
if ($menu_item['link']['hidden'] == 0) {
     
// Build class name based on menu path
      // e.g. to give each menu item individual style.
      // Strip funny symbols.
     
$clean_path = str_replace(array('http://', '<', '>', '&', '=', '?', ':'), '', $menu_item['link']['href']);
     
// Convert slashes to dashes.
     
$clean_path = str_replace('/', '-', $clean_path);
     
// Clean a node path for the c_menu_teaser function
     
$node_path = str_replace('node-','',$clean_path);
     
$path_class = 'menu-path-'. $clean_path;
     
// If it has children build a nice little tree under it.
     
if ((!empty($menu_item['link']['has_children'])) && (!empty($menu_item['below']))) {
       
// Keep passing children into the function 'til we get them all.
       
$children = theme('nice_menu_build', $menu_item['below']);
       
// Set the class to parent only of children are displayed.
       
$parent_class = $children ? 'menuparent ' : '';
       
$output .= '<li id="menu-'. $mlid .'" class="'. $parent_class . $path_class .'">'. theme('menu_item_link', $menu_item['link']);
       
// Build the child UL only if children are displayed for the user.
       
if ($children) {
         
$output .= '<ul>';
         
$output .= $children;
         
$output .= "</ul>\n";
        }
       
$output .= "</li>\n";
      }
      else {
       
$output .= '<li id="menu-'. $mlid .'" class="'. $path_class .'">'. theme('menu_item_link', $menu_item['link']) .'</li><li class="mt_teaser">' . c_menu_teaser($node_path) . '</li>'."\n";
      }
    }
  }
  return
$output;
}
?>

Thank you, this works great!

Thank you, this works great!

np

glad it helped :)

Hi, thanks for the code.

Hi,
thanks for the code. So, if I want to creat a block with cck field from the node. I can do it with following code?
Instead of menu_teaser I just put my field name? Do I need to get the nid? What does this $node_path does?

thanks

function c_menu_teaser($node_path) {
$thenode = node_load($node_path);
if (!empty($thenode->field_menu_teaser[0][value])) {
$menu_teaser = $thenode->field_menu_teaser[0][value];
return $menu_teaser;
}
}