Or do I have to set it somewhere else?

The context is that I am using an ajax callback function in a payment content type to set the values of a select list based on the value of another select list. The first is a select list of properties for a particular landlord, and on choosing a property the second select list is populated with all the charges related to that property. This then allows a payment to be allocated to a given charge, and so the charge is recorded in an Entity Reference field, that links that payment to the charge it was allocated to. I put this all into a fields collection as it is possible to divide a payment between various charges, for example if you are paying the service charge on a few properties that you own.

Being new to drupal and having encountered some difficulties adding the ajax callback to a element in a collection field, I chose to implement this by adding the two select lists through a call to _form_payment_node_form_alter() and hook_node_submit() to copy the nid value of the relevant charge instance to the collection field element ['target_id'] value.

After doing so I use dpm($node) to check the value is set correctly and see that all is in order.

The two payment allocation fields have the same value

You can see the field_payment_charge_allocation field (my added field) has the same value as field_payment_charge_allocate['und'][0]['target_id']. Good!

I also look at the node in hook_node_insert() but by now the field collection has been replaced with the target id for the field collection instance and so I can no longer see the value through the $node variable. Instead I use

$fc = entity_load('field_collection_item', 

Wrong value on load

This is the value at insert. As you can see it has changed. ¬¬

Somewhere between these two events I lose the value I have assigned. The value that is saved is whatever value the field collection element has set through its widget. As I have set its #access parameter to FALSE this usually means no value is recorded. (I have tested it by setting a value through the widget on the add content form too, and it saves whatever value I set.)

Where in my module should I set this value so it gets saved along with the rest of the data on the form? Also, out of interest, where in the work flow is this value getting changed? How can I delve deeper into the workflow to see what goes on with related entities when manipulating the the node forms?

If you got this far, thanks for reading my long winded question.

UPDATE: I found this issue open in the field collection documentation that might be related. I will investigate.

I am currently fixing this issue by setting $_SESSION['node'] = $node in hook_submit_node() and then setting the field collection element to the value I want in hook_entity_presave()

 * Implements hook_entity_presave().
function CPSmodule_entity_presave($entity, $type) {
  switch ($type) {
    case 'field_collection_item':
      $node = $_SESSION['node'];
      $entity->field_payment_charge_allocate[LANGUAGE_NONE][0]['target_id'] =     

This is a very unsatisfactory fix. I would appreciate any help in understanding this issue and fixing it correctly :)

  • Is the field collection field field_payment_allocate attached to your payment node? If that is the case then the node submit process might be creating a new field collection item when you are saving the node. – Sudheesh Damodaran Jan 9 '14 at 9:29
  • Not sure what you mean. The field collection is a field in the payment content type, yes, and has two elements, field_payment_amount_to_allocate and field_payment_charge_allocate. Do you mean the node_submit() function from the node api, or from the field collection api? I ran grep -rnw '.' -e "submit" in the field collection folder and could find no implementation of hook_node_submit() – NectarSoft Jan 9 '14 at 13:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.