I have observed the following error in my logs:

Invalid argument supplied for foreach() in /.../sites/default/modules/views/views_query.inc on line 123.

views_query.inc on line 123 looks like:

foreach ($view->sort as $i => $sort) {

This is in function _views_view_build_sorts(). I guess it is called even when no sort options have been specified?

I have fixed my copy by adding a check at the begining of that function:

/*
 * Add sorting information from a view to a query.
 */
function _views_view_build_sorts(&$query, $view) {
  if (empty($view->sort)) {
    return;
  }
  $sorts = _views_get_sorts();

  foreach ($view->sort as $i => $sort) {
    ...
    ...
    ...

Comments

merlinofchaos’s picture

Status: Needs review » Needs work

This isn't the correct solution;

When a $view is loaded or otherwise created from within views, it should be sanitized to ensure that all arrays exist. This means that somehow a view is getting to this part of the code without this sanitize function being run on it, and that could indicate a bigger problem than simply the array not existing.

One more thing to look into when I have more free time again.

markus_petrux’s picture

I've found the same error related to the foreach loops in _views_view_build_filters() and _views_build_query().

It looks like, for some reason, _views_build_query() should perform some validations before doing its work.

This is probably my fault when calling the views_build_view() API with an invalid $view.

merlinofchaos’s picture

Ahh, this likely means that views_build_view should be and is not calling...I think it is _views_check_arrays() or something like that. Which is a good idea since a view can come from an external source and we should ensure that the view is valid.

markus_petrux’s picture

I found my error. I was not checking if the result from calling views_get_view() was valid.

Well, I hope my mistake helps someone else. :-)

merlinofchaos’s picture

Title: Invalid argument supplied for foreach() views_query.inc on line 123. » views_build_view() crashes with: "Invalid argument supplied for foreach() views_query.inc on line 123." when invalid view given.
Category: bug » support
Priority: Normal » Minor
Status: Needs work » Active

Hmmm. Make me wonder if I should make views_build_view a little smarter and check that, or at least crash in a way that makes it obvious that the view was invalid.

I'm torn on this one, and will ruminate on it for awhile.

merlinofchaos’s picture

Status: Active » Closed (won't fix)

Decided that programmer error is just going to lead to stuff like this.