I added a views_php field to my view
I get the following with the offset incremented for each row ie. Undefined offset: 1, 2, 3 depending on the row

Notice: Undefined offset: 0 in views_php_handler_field->render() (line 218 of modules/all/views_php/plugins/views/views_php_handler_field.inc).
Warning: Invalid argument supplied for foreach() in views_php_handler_field->render() (line 218 of modules/all/views_php/plugins/views/views_php_handler_field.inc).

I think it is an off by 1 error.

$this->view->row_index returns 1 for the first row
but $this->view->style_plugin->rendered_fields[1] is not defined instead $this->view->style_plugin->rendered_fields[0] is.
function render($values) {
    // Execute output PHP code.
    if (!empty($this->options['php_output']) && isset($this->php_output_lamda_function)) {
      $normalized_row = new stdClass;
      foreach ($this->view->style_plugin->rendered_fields{$this->view->row_index} as $field => $value) {   <----- offending line 218
        $normalized_row->$field = $value;
      }

Comments

VenDG’s picture

I am also getting the same error.

kenheim’s picture

Did you find a solution? My php code is functioning fine - outputting what I want, but these error messages are of some concern.

ajlow’s picture

*bump* I am too...

ajlow’s picture

StatusFileSize
new8.72 KB
new8.79 KB

Doing some investigation and I found something interesting.

If I had my php field as the first field in the list of fields, I get the error:

Only local images are allowed.

If, however I moved the php field down the list of fields, it is ok:

Only local images are allowed.

It is strange but it seems to work for me...

kotoponus’s picture

StatusFileSize
new50.79 KB

SocialNicheGuru's description spot on.

In order to see the values of the variables in focus here in line 218, I have done the following:

dsm($this->view->row_index);
dsm($this->view->style_plugin->rendered_fields);
dsm($this->view->style_plugin->rendered_fields{0});
        foreach ($this->view->style_plugin->rendered_fields{$this->view->row_index} as $field => $value) {

You see:

debug message

It is clear that the $this->view->row_index is supplying 1 when there is no index 1 in $this->view->style_plugin->rendered_fields. So, if you force feed the index (at 3rd debug), it is spitting the value hence no error message to be produced.

So, my first guess is either the index should be returning 0 instead of 1 as SocialNicheGuru says or $this->view->style_plugin->rendered_fields to contain an additional value for index 1. Considering this particular value set should appear as a first row in this particular view, index 0 seems correct. In which case, the index should be fetching 0 rather than 1?

kotoponus’s picture

StatusFileSize
new806 bytes

Ok, the thing is, it does return the result I want. It just returns the error message for me as well.

Looking at the behaviour, $this->view->style_plugin->rendered_fields acts like an accumulator of all the rows to go through. So, "CSR" comes first for me and the second row value "ABC" comes next. I see the error at the point of processing "ABC" row, therefore, I only see CSR at index 0 in $this->view->style_plugin->rendered_fields. And this makes sense from the comment from SocialNicheGuru:

I get the following with the offset incremented for each row ie. Undefined offset: 1, 2, 3 depending on the row
...
I think it is an off by 1 error.

This means we will never enter this foreach loop. But the codes further down produces the final product $value for "ABC" which is an rendered HTML for ABC.

Maybe there is a case when we will actually see the routine enters foreach loop, but, at this point, I say the loop routine should be ignored if $this->view->style_plugin->rendered_fields{$this->view->row_index} is not set.

Like,

if (isset($this->view->style_plugin->rendered_fields{$this->view->row_index})) { 
    foreach ($this->view->style_plugin->rendered_fields{$this->view->row_index} as $field => $value) {
        $normalized_row->$field = $value;
    }
}

Or something more elegant if anyone can come up with it.

What do you view_php maintainers think?

kotoponus’s picture

Status: Active » Needs review
kotoponus’s picture

Component: Code » Field Handler
ismail cherri’s picture

Hi,
The patch in #6 fixes the issue.
In order to reproduce the error, the PHP field needs to be the first field; you can also fix the error without patching by placing the PHP field a second or more.

joelpittet’s picture

I tested #6 and it works as described in #9

I wonder why row_index is 0 when the PHP field is second position yet, starting at 1 when it's in the first position.

jrochate’s picture

So many years later, it's still valid :)
Thanks for sharing.