I am receiving the following error on some views which contains a field collection which is empty.

Undefined offset: 0 in views_handler_field_field->set_items() (line 725 of /local/drupal-7.2/sites/all/modules/views/modules/field/views_handler_field_field.inc)

It does NOT appear on blocks. However, if the view is a list or a table, the warning appears for each data set that DOES NOT have the field collection data. So, If I have three users, and only one has data in the field "affiliations" (Title and Institution), then I receive two errors. If I fill in the fields for other users, the errors disappear. See also this issue:

http://drupal.org/node/1153066

Comments

OnkelTem’s picture

Version: 7.x-1.x-dev » 7.x-1.0-beta2
StatusFileSize
new504 bytes

Confirmed.

I believe problem initiates in field_collection_field_formatter_links() which doesn't check $items and when its empty still adds data to the $element array but with index -1:

if (empty($element['#suffix'])) {
    $index = count(element_children($element)) - 1; // would be -1 if element_children() returns empty result
    $element[$index]['#attributes']['class'][] = 'field-collection-view-final';
}

So we having $elements array with the nice element:

-1	Array [1]	
	[0...0]	
		#attributes	Array [1]	
			[0...0]	
				class	Array [1]	
					...

Later in field.default.inc:232

  $addition[$field['field_name']] = array_merge($info, $elements);

merging produces empty element:

0	Array [1]	
	[0...0]	
		#attributes	Array [1]	
			[0...0]	
				class	Array [1]	
                                        ....

Finally in views/modules/field/views_handler_field_field.inc.views_handler_field_field->set_items(), $render_array is now containing this element which makes element_children($render_array) to return key as there were a proper element to render, while there's not.

    foreach (element_children($render_array) as $count) {
      $items[$count]['rendered'] = $render_array[$count];
      $items[$count]['raw'] = $render_array['#items'][$count]; // this line trigger's Notice
    }

Many words, but the patch is trivial. Attached.

OnkelTem’s picture

StatusFileSize
new2.2 KB

Updated patch. Things go a bit tricky.

To get anything rendered in view fields, $render_array from
views/modules/field/views_handler_field_field.inc.views_handler_field_field->set_items()
should has both #items and corresponding renderable children.
In case when we have Add link but have no items, it will never be rendered, since it dones't produce good child.
So we can add fake child.
See the patch.

OnkelTem’s picture

Status: Active » Needs review

Status: Needs review » Needs work

The last submitted patch, view_collection_render_fix.patch, failed testing.

OnkelTem’s picture

StatusFileSize
new2.33 KB

Oops, old school patch failed.
Giving a try to new style, created with diff -upr.

chrisolof’s picture

Version: 7.x-1.0-beta2 » 7.x-1.x-dev
Status: Needs work » Needs review
StatusFileSize
new2.19 KB

The code changes from #5 fixed the issue for me. Attached is a patch with the same changes from #5, but created with "git diff."

vasike’s picture

i can confirm this issue and the last solution #6 worked for me. thank you

jojonaloha’s picture

I've also experienced this issue and the patch in #6 solved this error message for me.

torotil’s picture

Status: Needs review » Closed (cannot reproduce)

The original patch doesn't apply anymore. Also I cannot reproduce the originally reported bug anymore. It seems that the patch had to wait too long in the issue queue and became stale.