I've written my php code so as to grab some content from field collection fields on the node -- the php code works correctly when I simple 'edit' the node and 'save' the node manually --- but if I try to batch process a number of nodes (either with admin/content or using a vbo) the field collection fields are not being retreived correctly (and so not being integrated into the title). my code works when I edit each node, one at a time, but I have almost 4000 nodes of this content type that need to have their titles updated....

The field collection id is easy to get

$foo_entity = $node->field_foo['und'][0]['entity'];
$foo_entity_id = $foo_entity->item_id;

but then -- getting the data for the content in that entity seems harder -- I'm using:

$foo_entity_load = field_collection_item_load($foo_entity_id);
if (!isset ($foo_load->targetfield['und'][0]['value'])){
$foo_load->targetfield['und'][0]['value']="";}
$target =$foo_load->targetfield['und'][0]['value'];

Any thoughts on why this would work when editing a single node...but not with batch or vbo? Thanks

Comments

verres’s picture

using dpm() - I've noticed that when I use the 'node edit' 'node save' method - the full field collection (and all the fields therein) are visible for the php code in autonodetitle area. So when the nodes are saved one by one, they field collection items are correctly placed into the title.

however, during batch or vbo -- the dpm indicates that the only thing that is visible to the php code is the entity id for the field collection entity.

so - I included in my php code an effort to get the field values in the field collection visible to the code - and used this:

$foo_id = $node->my_field_collection_field['und'][0]['value'];
$foo_entity_load = field_collection_item_load_multiple(array($foo_id));
dpm($foo_entity_load);

and this shows me that the field data is now visible -- but I can't get the field values out of the $foo_entity_load object. I've tried: field_get_items() --

$x = field_get_items('field_collection_item',$foo_entity_load,'field_I_want_value_for');

but that didn't seem to work -- I see a few posts related to entity wrappers - but I keep thinking there must be a simple function to get those values out of the object... thnx

verres’s picture

ok, so now I'm realizing that in one case (the basic node edit form), the field collection items are embedded in the $node (so can be accessed directly) - but in the bulk edit options, only the entity id is available. I need my php code to work under both situations -- so don't want to use the embedded values. I'd be better served by grabbing the field collection ID and then using field_collection_item_load() based on that ID number to retreive the field collection items. This function seems to return an object (not an array?) so I'm not sure how to get the field collection item values out of the returned object so they can be added into the autonodetitle via 'print $xxx'.

dpm() of the returned object from the field_collection_item_load() displays:

... (Object) FieldCollectionItemEntity
$...
item_id (string, 5 characters) xxx*
[field_name_1] (array 1 element)
[und] (array 1 element)
[0] (array 1 element)
[value] (string, 2 characters) 23

*xxx is the specific entity_id number for the particular field collection item being retreived.

I need the '23' to be cast as a string variable for use in the autonodetitle formula...

verres’s picture

day two ends... failure.... so frustrating to see the value displayed by dpm() in the message section of the page and not be able to grab it... I've now tried (unsuccessfully) to use entity wrappers to try to get a way to grab the value out of the 'thing' being produced by the field_collection_item_load() function; but cant seem to get it right. I need something like field_get_items() but so far, I haven't been able to make that work because at best, I can only get an empty array back...

the specs for field_get_items is:

$foo = field_get_items($entity_type, $entity,'field_name_of_desired_field')

I've been using 'field_collection_item' for $entity_type
and the then for $entity, I've been putting the result from my field_collection_item_load() call,
and for the field name, I'm using the fieldname that shows up in the
dpm(field_collection_item_load($insert_entity_id_here))

hoping an awesomely better drupaler than me will send me a crumb of help -- thnx

ashopin’s picture

Issue summary: View changes

Hi verres,

I was able to print field content from a field collection using this method:

$items = field_get_items('node', $node, 'field_applicant');
$first = '';
$last = '';
foreach ($items as $item) {
  $fc = field_collection_field_get_entity($item);
  $first = $fc->field_applicant_address['und'][0]['first_name'];
  $last = $fc->field_applicant_address['und'][0]['last_name'];
}
print $first . $last;
fadgadget’s picture

Hi i was wondering if anyone can put me out my pain over trying to print a field collection item as the title and to truncate it. Ive tried following the above code with no success. all help very much appreciated to get mer out my hell. thanks

field collection name = field_suggestion
field collection field name = field_suggested_stuff

$mystring = '[node:field_suggested_stuff]'; 
return drupal_substr($mystring,0,45)."..."; 
gaurav.kapoor’s picture

Status: Active » Closed (outdated)