I created a views page to display a taxonomy term view using the "Panels: Teasers 1 top + 3 columns". When viewing a term with only 1 node assoiciated to it I got the following error.

warning: Invalid argument supplied for foreach() in \sites\all\modules\views_bonus\views_bonus_panels.module on line 37.

I fixed it by adding an is_array check before the foreach statement.

   if(is_array($items)){ // ** added is_array to stop foreach attempting to process 0 item array.
      foreach ($items as $item) {
        switch ($count % $cols) {
          case 0:
            $section = 'left';
            break;
          case 1:
            if($cols == 2) {
              $section = 'right';
            }
            else {
              $section = 'middle';
            }
            break;
          case 2:
            $section = 'right';
            break;
        }
        $content[$section] .= $item;
        $count++;
      }
    } //

Comments

nevets’s picture

This should also work as the error is normally not actually about arrays with no items but a non array variable so changing

foreach ($items as $item) {

to

foreach ((array)$items as $item) {

should also work (it casts $item to an array if it is not already one)

dmitrig01’s picture

Category: task » bug
Priority: Normal » Minor

@nevets: would you mind providing a patch?

dmitrig01’s picture

Status: Active » Fixed

Fixed.

Anonymous’s picture

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for two weeks with no activity.