I have a node with field1 and field2 (field_collection)

I am using hook_entity_presave($entity, $type) like this:

function txs_entity_presave($entity, $type)
    if ($type == 'field_collection_item') {
      ...do some stuff

For now, this hook is called firstly for field1 and, only after field1 is saved, this hook is called for field2.

I would like the opposite because I need to retrieve the value of field2 to handle the "field1_presave".

Here are the details:

Here is the code inside the hook_entity_presave, when field1 is being presaved. This code is showing why I need that field2 must be saved before field1

if ($entity->field_name == 'field1'){
    $entity_w = entity_metadata_wrapper('field_collection_item', $entity);
    $node = $entity_w->host_entity->value();
    $node_w = entity_metadata_wrapper('node', $node);

    //HERE I NEED THAT field2 was already saved...
    //This is not the case... 
    //the following line does not works in all cases
    //as field2 is not yet saved, $used_value could be wrong
    if ($used_value=='something'){
        ...change something in field1

1 Answer 1


The only way I could solve my problem was to handle the logic in the form_validate function. Unfortunately, I couldn't update the field collection from there... this is why I added a validation function for my field collection:

 * Implements hook_field_widget_form_alter().
function MYMODULE_field_widget_form_alter(&$element, &$form_state, $context) {
  // Add custom validation hooks for Field Collections.
  if ($context['field']['field_name'] == 'field1') {

Then I could insert my logic there:

 * Custom validate function for field_assignments.
 *  @see MYMODULE_field_widget_form_alter()
 function MYMODULE_field1_validate($element, &$form_state, $form) {   
    if (...){ 
       form_set_value($form["field1"][LANGUAGE_NONE][0]['field_to_change'], array(LANGUAGE_NONE => array(0 => array('value' => 123.456))), $form_state);

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.