1

New to Drupal and I'm assuming the answer is simple, I've looked through the test suites etc. and still missing the point it seems.

I have a node with a field collection (field_theatre_date_set) that will store a number of dates and times. I have loaded the node and the collection is there but it's empty.

I have used various forms of this and an obvious problem is that the $field_collection_item_value below array structure just isn't there.

Any further pointers greatly received.

$loop=0;
foreach($showing_array as $key=>$value){

 $date='xxxx';
 $time='xxxx';

 $field_collection_item_value = $node->field_theatre_date_set[LANGUAGE_NONE][0]['value'];      
 $field_collection_item = entity_load('field_collection_item', array($field_collection_item_value)); // Load that field collection item 

 $field_collection_item->field_theatre_date_set_date[$node->language][$loop]['value'] = $date;          
 $field_collection_item->field_theatre_date_set_time[$node->language][$loop]['value'] = $time;
 $loop++
}
 $field_collection_item->save();

/**Update***/

Progress has been made. I still have an empty field_collection reference that needs to store a date and time.

I now understand that field collections store a value and a revision_id. I now need to work out how to store these revisions. I have the following code:

$field_collection_item = entity_create('field_collection_item', array('field_name' => 'field_theatre_date_set'));

$field_collection_item->setHostEntity('node', $node);
$field_collection_item->date[LANGUAGE_NONE][]['date']   = $date;
$field_collection_item->field_description[LANGUAGE_NONE][]['time']   = $time;               
$field_collection_item->save();

Now when I output the field_collection_item I see the following where I am expecting to see the the value and revision:

[field_theatre_date_set] => Array
            (
                [und] => Array
                    (
                        [0] => Array
                            (
                                [entity] => FieldCollectionItemEntity Object
 *RECURSION*
                            )

                    )

            )

And at the very bottom of the array I have the dates and times wallowing on their own:

[date] => Array
    (
        [und] => Array
            (
                [0] => Array
                    (
                        [date] => 2014-03-13
                    )

            )

    )

[time] => Array
    (
        [und] => Array
            (
                [0] => Array
                    (
                        [time] => 19:45:00
                    )

            )

    )

Any pointers on how to move them into my field_theatre_date_set if they aren't expecting date and time values?

Thanks all for looking!

2 Answers 2

1

I believe you need to load and save the node to which the field collection is attached.

This question: Modifying a field collection programmatically missing hostEntity fields is slightly different but the examples are accomplishing the same thing.

1
  • Thank you for the reply, I will take a look. I thought by using $node->language I was doing just that, I will review the other question and report back. Commented Jul 15, 2014 at 10:06
0

try using the EntityAPI to load the node and inspect it using the Devel module. The EntityAPI will also make creating/editing the field collection easier.

There are lots of ways to save or load a field collection, for examples see: https://www.drupal.org/node/1393552#comment-5428548

or any of the various answers here: How do I load a field collection from a node?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.