1

Created a module (D7) that turns every nodes edit tabs into contextual links instead. Works great... But on the first initial load to the page I get a warning. Disappears after I refresh once. Then it comes back around surfing to another page and back again.

Warning: Invalid argument supplied for foreach() 
in mytheme_contextual_contextual_links_view_alter() 
line 19....

Driving me bonkers. Code below. It's having an issue with my foreach($tabs as $tab)...

  function mytheme_contextual_block_view_system_main_alter(&$data, $block) {
      if (user_access('access contextual links')) {
        $tabs = menu_primary_local_tasks();
        $data['content']['#contextual_links']['block'] = array(
          'admin/structure/block/manage',
          array('system', 'main'),
        );
      }
  }

  function mytheme_contextual_contextual_links_view_alter(&$element, &$items) {
      if (arg(2) != 'edit' && arg(3) != 'edit' && arg(0) != 'admin' && arg(1) != 'add') {
        if (isset($element['#element']['#block']) && $element['#element']['#block']->delta == "main") {
          $tabs = menu_primary_local_tasks();
          foreach( $tabs as $tab ){
              $element['#links'][$tab['#link']['path']] = array(
                'title' => $tab['#link']['title'],
                'href' => url($tab['#link']['href'], array('absolute' => TRUE)),
              );
          }
        }
      }
  }

  function mytheme_contextual_preprocess_page(&$variables) {
      if (arg(2) != 'edit' && arg(3) != 'edit' && arg(0) != 'admin' && arg(1) != 'add') {
        $variables['tabs']['#primary'] = array();
      } 
  }
  • try adding print_($tabs); or dpm($tabs); if you have devel installed before the foreach so you can see the content of the variable before the for loop tries to use it. – Daniel Harper Oct 30 '13 at 9:08
1

Place the for loop inside an if statement

$tabs = menu_primary_local_tasks();
if($tabs)
{
      foreach( $tabs as $tab ){
          $element['#links'][$tab['#link']['path']] = array(
            'title' => $tab['#link']['title'],
            'href' => url($tab['#link']['href'], array('absolute' => TRUE)),
          );
      }
}
  • @MohammedShameen Worked! Thanks. What was the approach you took on this? Just that it needs to be forced to loop to guarantee the menu items are fully ran through? – tdm Oct 30 '13 at 14:01
  • 1
    Just made sure that the $tabs has value. If it is blank then there is no point in looping through it. It would through an error. – Mohammed Shameem Oct 30 '13 at 14:22
  • Can this code be implemented as a module on its own without having to modify themes? Although the question says module it looks like themes must be created or changed to work with it. – vfclists Dec 6 '13 at 2:43

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.