Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
I am submitting a form via AJAX to a custom menu hook for processing. With every other node element I can simply set the value of the field based on the REQUEST values sent..
Eg:
$node->field_hello[LANGUAGE_NONE][0] = 'world';
and then call node_save($node). I thinking your module relies on using the _validate and _submit hooks to work properly? I've tried the following with no luck:
$form_state = array();
$form_state['values'] = $values; //values from request object
$form_state['build_info']['args'] = array($node);
try {
drupal_form_submit('form_field_profile_exp_type', $form_state);
} catch (Exception $e) {
dpm(array('error:'=>'Failed to save new data. Check the log for details.', 'Exception'=>$e));
$return = _fp_ipe_error();
}
dpm(form_get_errors(), 'form errors ?');
Any ideas?
Comments
Comment #1
the_g_bomb CreditAttribution: the_g_bomb commentedDoes this help?
http://rajanmayekar.com/blog/programmatically-creating-deleting-modifyin...
you could also see the discussion in #1179088: Dynamically add a “Field Collection” by script?
Comment #2
the_g_bomb CreditAttribution: the_g_bomb commentedChanging the priority as support requests are never "major", see Priority levels of issues for details.
Comment #3
dynamicdan CreditAttribution: dynamicdan commentedNot really..
There is a lot of support on "adding field collections to nodes/entities" and "saving/deleting field collection field data in code" but they are not really what I need.
My scenario:
I have a custom form making use of the node edit form and extracting a subsection of fields. In this case just the field collection fields...
At the moment, I'm having to re-implement client side logic for adding a new entry or removing an entry. That logic is partially working.
This works for updating existing values:
I'm assuming that if I send the correctly formatted $_REQUEST/POST values then all entries should be updated/deleted/added (CRUD). Only RU is working, CD not yet. I'm not sure how to get it to recognise new entries or delete old ones without emulating the built in field collection module ajax callbacks. I can't really use those ajax callbacks from the module in my custom AJAXified setup.
Comment #4
mikemadison CreditAttribution: mikemadison commentedI'm running into a similar problem and dynamicdan I'm curious if you found a solution?
I have a custom / dynamically generated list field that works perfectly well when it is stand alone. As soon as I dump it inside a field collection, I am not able to actually save any of the selections that the user makes. I still see my custom list item, but even in the pre-save hook, there is nothing captured when the user hits the submit button.
Comment #5
dynamicdan CreditAttribution: dynamicdan commentedI still don't have a solid solution.
I have trouble with adding and deleting entries. I basically need a pure AJAX solution for my system.
Comment #6
mikemadison CreditAttribution: mikemadison commentedI've added a fairly detailed writeup over on the normal forums about what I'm trying to do. https://drupal.org/node/1854524
Comment #7
dynamicdan CreditAttribution: dynamicdan commentedI hava a solution..
On the client side I do some fancy custom jquery clone stuff to get a new entry for the user. The new entries have no _weight property (weights are not needed for me). By default, the first empty row saves fine on it's own but the new ones need some help...
I think this has something to do with some default FC entity preparing it does in my standard 'field_attach_form()' node form.
Then I do my normal save stuff as previously mentioned..
Deleting is handled client side by simply clearing the input values for the fields and then hiding the rows (so that they can still be submitted).
So I have C, U, D. I guess R is working in the CRUD.
Would have been nice is there were some standard functions/hooks provided like hook_field_collection_save_form() and standard client side JS to insert a new entry.
FYI, here's my client side JS for cloning a row:
Comment #8
mikemadison CreditAttribution: mikemadison commentedI've done a bit of experimenting here, and I did have some progress submitting against a field collection with a custom field. In my case, I was able to establish a list of allowed values ahead of time (something like 40-50 different values) and depending on the user, I would show that user a subset of values. So my steps are something like...
1. Create Content Type
2. Add Field Collection Field
3. Add List (Text) Field to Field Collection
4. Add Allowed Values List to Field Collection Field
5. Use code below to modify the Field Collection Field to only show certain values to the user when they pull up the form.
In doing it this way, all of the core field code still kicks in and works as intended, I'm just altering the display of what the user is seeing when they are interacting with the form. Beyond that, the field is still a legit field and works the way Drupal intended. Works in this case, may not work in all if you aren't using a list field.
In the example above, the "field_goal" is my field collection and the "field_goal_category" is my custom list field. I am essentially replacing ONLY the values in the field with my custom $values array (defined outside of this code) and then returning the form without monkeying around with anything in the database.
The user makes a selection, which is valid, and we move on with out lives.
Comment #9
jmuzz CreditAttribution: jmuzz commentedIt sounds like solutions were found.