I have a few field collections that are included in a content type. When I create the a node of that content type, the data from the field collection areas do not save. There is no data in the database for them either. It's like they're skipped.

If I add them in after the initial creation, they show up fine.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

RyanPrice’s picture

Anyone else have this issue? I'm a little surprised this isn't happening to others as well.

rfc2460’s picture

I have the same issue. Very strange, it occurs only for one content type.

rfc2460’s picture

@RyanPriceDotCa Do you have conditional_fields module ?

The issue I have is related to a required field that is normally handled by conditional_fields.

For me it is not a bug in field_collection but a problem of compatibility with conditional_fields.

rfc2460’s picture

See #1582094: Incompatibility with Field collection for the explanation of my problem...

RyanPrice’s picture

I do have conditional_fields installed, but none of my field collections are dependent or dependee. I'll investigate this a bit further as well and leave a comment on your other ticket.

Thanks for posting here to let me know.

rfc2460’s picture

You do not need to have a direct link between your field collection fields and the dependents or dependees. The problem is that when there is an error in the form, the field collection is not saved even if the error is not related to the field collection.

In function field_collection_field_widget_embed_validate (see code snippet below), it tests if there are errors in the form and not only the error coming from the validation of the field collection field. As we can have errors coming from required field hidden using conditional field, then the field collection is not saved. The trick is that conditional field module will manage (and clean) the errors relative to the dependents or the dependees but it is called at the end of the form validation process.

function field_collection_field_widget_embed_validate($element, &$form_state, $complete_form) {

  // .... 
  // Only if the form is being submitted, finish the collection entity and
  // prepare it for saving.
  if ($form_state['submitted'] && !form_get_errors()) {  // <---- HERE IS THE ISSUE
    //...
  }
}

So I rewrite this function in order to check only the errors coming from the field collection validation. I'm really sorry but I am not familiar with the patch process and I unfortunatly do not have time before the end of the week so here is the code I use now (and that solves my problem...)

function field_collection_field_widget_embed_validate($element, &$form_state, $complete_form) {
  $instance = field_widget_instance($element, $form_state);
  $field = field_widget_field($element, $form_state);
  $field_parents = $element['#field_parents'];
  $field_name = $element['#field_name'];
  $language = $element['#language'];

  $field_state = field_form_get_state($field_parents, $field_name, $language, $form_state);
  $field_collection_item = $field_state['entity'][$element['#delta']];

  // Attach field API validation of the embedded form.
  field_attach_form_validate('field_collection_item', $field_collection_item, $element, $form_state);

  // Variable to store the validation errors for the field collection
  // Errors are not directly set on the form to avoid compatibility issue with other modules
  // such as conditional_fields
  $field_collection_errors = array();
  // Now validate required elements if the entity is not empty.
  if (!field_collection_item_is_empty($field_collection_item) && !empty($element['#field_collection_required_elements'])) {
    foreach ($element['#field_collection_required_elements'] as &$elements) {

      // Copied from _form_validate().
      if (isset($elements['#needs_validation'])) {
        $is_empty_multiple = (!count($elements['#value']));
        $is_empty_string = (is_string($elements['#value']) && drupal_strlen(trim($elements['#value'])) == 0);
        $is_empty_value = ($elements['#value'] === 0);
        if ($is_empty_multiple || $is_empty_string || $is_empty_value) {
          if (isset($elements['#title'])) {
            $field_collection_errors[] = array('elements' => $elements,
              'message' => t('!name field is required.', array('!name' => $elements['#title'])),
            );
          }
          else {
            $field_collection_errors[] = array('elements' => $elements,
              'message' => '',
            );
          }
        }
      }
    }
  }

  // Only if the form is being submitted, finish the collection entity and
  // prepare it for saving.
  if ($form_state['submitted'] && sizeof($field_collection_errors) == 0) {

    field_attach_submit('field_collection_item', $field_collection_item, $element, $form_state);

    // Set the _weight if it is a multiple field.
    if (isset($element['_weight']) && ($field['cardinality'] > 1 || $field['cardinality'] == FIELD_CARDINALITY_UNLIMITED)) {
      $item['_weight'] = $element['_weight']['#value'];
    }

    // Put the field collection item in $item['entity'], so it is saved with
    // the host entity via hook_field_presave() / field API if it is not empty.
    // @see field_collection_field_presave()
    $item['entity'] = $field_collection_item;
    form_set_value($element, $item, $form_state);
  }

  // Add the errors to the form
  foreach ($field_collection_errors as $field_collection_error) {
    form_error($field_collection_error['elements'], $field_collection_error['message']);
  }
}
mindaugasd’s picture

FileSize
74.17 KB

My conditional fields are not saved though I don't have "Field collection" on my website.

1. For testing I disabled **all** the contrib modules, still not working.
2. I use conditional fields on profile and content type. On profile it is working ok, but not on content type.
3. Only one field is get saved from 10. I attached a picture of all the dependencies for you to see. The difference between those are their type. Field which works has a BOOLEAN type, whereas not working fields all has LIST(TEXT) type.

All fields are wrapped with very popular fieldgruop module, but now it is disabled.

mindaugasd’s picture

tested even more.
1. Tried the same thing on a different content type - not working.
2. if condition is "HAS value" and field is "LIST(TEXT) type, when things don't work. If I choose a different type, for example "checked", when things work.

But still not found a solution. I still need "has value" condition type. I guess I provided everything which is needed to reproduce a problem.

mindaugasd’s picture

Sorry, I thought this is a "conditional fields" issue queue :) I will create a seperate issue.

tim.plunkett’s picture

Priority: Major » Normal

Okay, so #6 needs to be turned into a patch. Not major though, since conditional_fields doesn't even have a release yet.

manuelBS’s picture

I have a problem which is very simliar. If I save the initial node with the filled field collection data is not saved (but on another field collection it is). I even cannot save the data of this field collection on another update of the node.

EDIT: I think it is a conditional fields issue because the values are not saved if my fields have certain conditional fields state.

Gemma Morton’s picture

This worked for me, thank you, herewith a patch as per #6.

rfc2460’s picture

Thanks mckeen... I'm sorry I really did not find time to do it.

RyanPrice’s picture

Status: Active » Reviewed & tested by the community

Works for me. Let's roll this into a release please maintainers.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, field_collection_conditional_fields_save.patch, failed testing.

RyanPrice’s picture

Version: 7.x-1.0-beta4 » 7.x-1.x-dev
Status: Needs work » Needs review
FileSize
2.52 KB

Attempted the patch myself. This patch should be applied from 7.x-1.x-dev

yannickoo’s picture

Status: Needs review » Reviewed & tested by the community

Thanks RyanPriceDotCa, works fine!

sgb9809’s picture

Hello,

The patch appears to save the field collection but throws notice: Undefined variable: field_collection_errors in field_collection_field_widget_embed_validate() (line 1323

Thanks

EDIT: FWIW there appear to be times where $element['#field_collection_required_elements'] is undefined, which then cause field_collection_errors to not be initialized. I'm working with collections including dates, entity references, and text fields, all required fields in the collection. Field validation in collections is definitely problematic, and compounded with conditional fields.

fago’s picture

Status: Reviewed & tested by the community » Needs work

I don't think that's a good way to proceed, we really should set general form errors *and* we should only update the field-collection entity if the complete form passes validation.

I think we should be using another step in the process to set the field-collection item, e.g. use hook_field_attach_submit() instead and move the code from
if ($form_state['submitted'] && !form_get_errors()) {
to it.

yenidem’s picture

I applied patch#16 to module, but it did not work. Is there any other solution?

I use field collection with conditional fields module.
thank you.

SuperHoman’s picture

Did this come to a resolution? I checked the latest dev code and do not see this patch implemented. Is this the direction that the developers are going with for the fix or is there a better way to address this?

rooby’s picture

@SuperHoman:
See fago's comment in #19

phpcitizen’s picture

Well still seems unresolved, an issue which makes such a great module dysfunctional.

jason.fisher’s picture

#16 was essential for allowing field collections to save on our profile2 entities. Thank you!

Karthick_s’s picture

Problem persists with the save_draft module also!

vivdrupal’s picture

As of Field Collection version 7.x-1.0-beta4, the problem persists.

Patch #16 by RyanPriceDotCa works.

Thanks

k.skarlatos’s picture

I have the same problem as well and can reproduce it at will. Patch #16 seems to work.
By the way this bug can even silently delete all existing field collection values from a node when using the edit form

3CWebDev’s picture

Patch #16 worked for me, thanks! Need a solution for core module though...

AbhijeetKalsi’s picture

I have also face the same issue. On add another field collection, the check box data is not save on first save. But work fine on next save. Any body have a solution ?

earwax’s picture

I also had this same problem and comment #6 explained it well.

In my case, I am using the save draft module, content lock, and the conditional field modules. My field collections were not saving. I realized there was a problem when I would go to edit a node, and then click cancel. The Content Lock module would kick in and unlock the node, but it also spit out some errors about particular required fields in my form that I never saw when submitting a form. Once I fixed these required fields in my form on my node, then the field collection was correctly saving again. I am using Field Collection 7.x-1.0-beta5 and did not apply any patches.

So in summary, there is basically a (silent) error happening somewhere on your form and you need to fix it before field collection will save.

Hope this helps others, and maybe that patch above works.

kunago’s picture

I must agree the current validation does create consistency issues, at least with the conditional_fields module which hides fields and those should not be validated.
The "field_collection_field_widget_embed_validate" function, however, does fire before "conditional_fields_form_validate" which results in this issue.

I tried to follow the suggestion in #19 but I failed to retrieve some variables used in the other function. I am not a good coder.

geek-merlin’s picture

@kunago #31: the plan to go is probably implementing #1781190: Field Collection saved on presave. i'm not perfectly sure which the best hook to use is NOR do i have much time. but feel free to pm me for assistance.

kscheirer’s picture

Status: Needs work » Needs review
FileSize
2.35 KB

I don't think the _presave issue has to do with this one - that's node operations, this form submit. I agree with fago in #19 - this code belongs in a _submit hook, not a _validate hook for forms. We should leave the real form errors alone - it's only field_collection's code that is saving data at the wrong stage.

Attached is a patch based on #19, moving the data saving code to a submit hook, not tested.

Status: Needs review » Needs work

The last submitted patch, 1549364-widget-validate-submit-33.patch, failed testing.

stefank’s picture

After applying the patch in #33 the node with field collection is trying to get saved and after a while I get error.

Fatal error: Maximum execution time of 60 seconds exceeded in.

Uncaught exception thrown in session handler.

PDOException: SQLSTATE[08S01]: Communication link failure: 1153 Got a packet bigger than 'max_allowed_packet' bytes: UPDATE {sessions} SET ...

Sorry forgot to mention that I've got the conditional fields active with DEPENDENT and DEPENDEES.

kscheirer’s picture

Assigned: Unassigned » kscheirer

My patch clearly does not work - but I'm looking for suggestions and improvements :) I'll take another crack at it when I have a chance. You can set max_allowed_packet to a larger value (usually 16M is good) in your my.cnf file, but I don't think that will resolve the problem you experienced.

presleyd’s picture

#7 worked for me. I don't really get Fago's suggestion in #19 though. Why is the double step needed?

rooby’s picture

Priority: Normal » Major

I would argue that this is major, because it silently causes data loss (a lot of data may be lost before a site admin realises that something is wrong), and even though conditional fields doesn't have a full release it is used by ~14000 sites.

geek-merlin’s picture

i think raising prio in #38 is the right thing here.
at #37: see #32.

Amruta_dani’s picture

#16 worked for me, Thank you guys

patchshorts’s picture

Verified #16 worked for me and my fc version is 7.x-1.0-beta5

jbenezech’s picture

#16 worked for me as well. Thanks

esolano’s picture

#16 worked for me too. Thanks a lot.

Heihachi88’s picture

Issue summary: View changes

After applying the patch from #16 i am getting following errors when trying to delete a node or add images to an image field:

Warning: Illegal string offset '#parents' in function form_error()
Warning: implode(): Invalid arguments passed in function form_error()

Notice: String offset cast occurred in function _field_invoke_multiple() 

Using php 5.4 by the way.

jamesmorrish’s picture

#16 almost worked for me, but was also generating two warnings:

Warning: Invalid argument supplied for foreach() in file_field_widget_submit() (line 772 of modules/file/file.field.inc).
Warning: array_values() expects parameter 1 to be array, null given in file_field_widget_submit() (line 779 of modules/file/file.field.inc).

The patch seems to be missing this line: $item = drupal_array_get_nested_value($form_state['values'], $element['#parents']);

jamesmorrish’s picture

Issue summary: View changes
Heihachi88’s picture

@jamesmorrish, did you get error's that i noted above with php 5.4?

balsama’s picture

Patch in #16 worked for me. I am running PHP 5.3 - so I couldn't test for the errors that @Heihachi88 reported. Note that #16 seems to apply cleanly to the 7.x-1.0-beta7 release. Not any of the dev releases.

ludwig.rubio’s picture

Is there something new about this? I’ve been searching for answer and I could't get a solution.

When we create a node whit field collection, in reality it is created as an “unconnected field collection”,then when we edit a node, the field collection is replaced per a new one, that really works.

The main thing is that the other field collection is kept as a unconnected field collection for ever :S

aescudier’s picture

Patch in #45 works for us on 7.x-1.x-dev - 2014-Apr-16
width multistep and conditional fields

ludwig.rubio’s picture

Thanks escudier, unfortunately It doesn't work for me. My case looks like another kind of incompatibility with tac_lite module.

Sebastien M.’s picture

Status: Needs work » Needs review
FileSize
586 bytes

This patch works in my own case. I've got field_condition which mask required fields. Those required fields generates form errors which disable node save.
Restricting "form_get_errors" to the current field element fixes the issue.

Hope it helps.

kiricou’s picture

it works for me,
thanks a lot

patchygreen’s picture

Works for me too. Thanks Sebastien.

RyanPrice’s picture

Status: Needs review » Reviewed & tested by the community

Thanks for the patch, looks good

The last submitted patch, 45: field_collection-fields-not-saving-1549364-45.patch, failed testing.

jmuzz’s picture

Status: Reviewed & tested by the community » Needs work

The patch likely works in many cases but I agree with #19, it doesn't get to the root of the problem. A validation hook is not the place to be doing data manipulation, especially operations that shouldn't be performed if there are errors on the form. In the case brought up the errors are eventually cleared by conditional fields so it seems to make sense to ignore the other form errors, but they could be valid form errors, or there could be another module that sets valid form errors after this hook is run. Another module could even set an error on the field collection item after this validate hook is called so the proposed version of the validation check in #52 can still miss the errors it is trying to check for. These operations should only happen if everything passes validation so they should go in a submit hook.

FloriNet’s picture

Yaaaaaay, thank you so much for this patch in #52, it works perfectly !

gkaz’s picture

It seems I had the same issue as Sebastien at #52.
I had some required fields enabled/disabled based on a radio field. It seems, having disabled required fields when submitting the form causes silent errors sometimes.

To avoid patching, my solution is to have those fields not to be required. Instead, I created a second dependency where I set the field as required along with enabling it.

cassien’s picture

i tried a lot of patches to avoid that probleme with conditionnal fields.

none of them worked...

i just found that solution : don't use 'insert value from widget', use 'all these values (AND)'

thentha18’s picture

#60 saved my life

cassien’s picture

haha

t14’s picture

#60 also worked for me. Thanks thentah18
Makes me wonder if the problem is actually in conditional fields module and not field collection

deeptanwar’s picture

Thanks RyanPrice,
Your patch worked for me...
Thanks a lot

ovi.indrei’s picture

Had same issue with Field Collection 7.x-1.0-beta10 and patch in #52 has worked for me.

yoshicn’s picture

Version: 7.x-1.x-dev » 7.x-1.0-beta8
Assigned: kscheirer » yoshicn
FileSize
586 bytes

#52 works perfect.

Adding patch for 7.x-1.0-beta8 as #52 could not be applied due to the module file difference between versions.

yoshicn’s picture

Version: 7.x-1.0-beta8 » 7.x-1.0-beta5
FileSize
586 bytes

One more patch for 7.x-1.0-beta5

deepakaryan1988’s picture

Status: Needs work » Needs review

Sending it to needs review.

jmuzz’s picture

Status: Needs review » Needs work

See #57 and #19.

frank.schalkwijk’s picture

I updated the patch from #16 to apply to the current 7.x-1.0-beta11 version.

kscheirer’s picture

Status: Needs work » Needs review

With a new patch, set status to "needs review"

tondeuse’s picture

Patch #52 resolves my issue right away. My conditional fields did not produce any validation errors before patching, but the data is not initially saved either, only on node update. Nothing is written to the db on upon node creation.

I tweaked all my conditionals to use specific values, instead of the 'Insert value from widget', as suggested in #60, just to be on the safe side.

kscheirer’s picture

@tondeuse how about with the patch in #70?

jmuzz’s picture

Status: Needs review » Needs work

Please see #57 and #19.

capysara’s picture

I know it may not be the "right" way to address the issue, and it's unlikely to make it into the module, but #52 still works for my purposes. I'm using entityforms, conditional fields, and field collections. (Also this is a really old issue for a module in beta that's being replaced by Paragraphs in D8.)

FWIW, This is also an issue with D7 paragraphs and they took a different approach, including the most recent patch is looking specifically for the conditional fields module.

ram4nd’s picture

Assigned: yoshicn » Unassigned
ram4nd’s picture

Status: Needs work » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.