2

I'm very new to Drupal and I'm receiving the error:

Cannot use object of type stdClass as array

in the following code:

$toolbar = $page['toolbar'];
foreach( $toolbar['menu_menu-smart-resources'] as $id => $object ) {
  $toolbar['menu_menu-smart-resources'][$id]['#localized_options']['attributes']['data-role'] = 'foo';      
}
print render($page['toolbar']);

Can someone help.

  • 1
    try print_r() to see the variable is array or object ? – niksmac Oct 22 '12 at 5:29
  • I have don, it never references stdClass, just array – Luke Snowden Oct 22 '12 at 5:34
  • if I kill the loop straight after it is declared the is no error, i.e. don't think it is a PHP error as such (i know it is) but maybe something todo with the render() function – Luke Snowden Oct 22 '12 at 5:37
  • Hello and Welcome to Drupal Answers! Where are you executing this code ? What are you trying to achieve using above code ? Give some depth about this question... – Anil Sagar Oct 22 '12 at 5:50
  • 1
    I want to add a attribute onto the <li>'s of a menu being generated. As I don't know drupal, I was just calling the above code in the page--{pagename}.tpl.php before the render() function – Luke Snowden Oct 22 '12 at 5:53
0

You are able to change menu attributes using Menu Attributes module . If any customization is needed on top of that then you can follow the below process.

You can able to override theme links using theme_menu_link in template.php. Follow below step to override.

  • Go to your active theme folder might be in sites/all/theme or if you are using default theme then themes folder
  • Go to the active theme folder
  • Look for template.php
  • Override theme_menu_link by copy pasting entire function and replacing theme with your theme name.
  • Do your changes inside function wherever necessary!

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.