If a fieldable entity is created but no default fields are added to the entity a foreach warning is thrown:

Warning: Invalid argument supplied for foreach() in _field_invoke() (line 188 of /Applications/MAMP/htdocs/textbookcentral/modules/field/field.attach.inc).

A simple if statement can fix this.

CommentFileSizeAuthor
fieldable_exception.patch3.2 KBeclipsegc

Comments

yched’s picture

Status: Needs review » Needs work

Strange we didn't notice it before.

I'd rather fix _field_invoke_get_instances() so that it always return at least an empty array - looking at the code, the case doesn't strike me.
Can you trace the content of $options when the error occurs ?

bnetsolutioninc’s picture

I'm using Drupal 7.0

I have created a custom module that have 2 major responsabilities:

- Create a custom content type by code (that is not using the field api)
- Import content for that specified content type from a csv file.

This module used the function: drupal_form_submit($form_id, $form_state)
to import the content.

When I use my module to import content, I have the following errors:

Invalid offset supplied in field.attach.inc at line 198.

After doing a lot of investigation, I found some interesting stuff in the function:

function _field_invoke($op, $entity_type, $entity, &$a = NULL, &$b = NULL, $options = array())

The above function is calling _field_invoke_get_instances($entity_type, $bundle, $options); (line 184)

This function return an array with all content type with all fields instance.

In my case, the Array returned for my content type is empty (no field instance in the array).

So, at line 189 of the _field_invoke function ($field_name = $instance['field_name'];) :
The instruction is not valid because the offset 'field_name' doesn't exist.

I suggest that the function _field_invoke should be modified as follow:

Insert an if statement between line 188 and line 189 to verify the validity of the 'field_name' offset.

line 188 foreach ($instances as $instance) {
if (isset($instance['field_name'])) {
line 189 $field_name = $instance['field_name'];

Thanks in advance !

Anonymous’s picture

Status: Needs work » Closed (cannot reproduce)

I do not think this is still an issue, SPARQL Views uses Entity API to create entities that don't have real fields or fake fields to start with and I don't get this Warning.