I have a form with a field collection "field_definition" which has 2 fields in it with unlimited cardinality on the field collection.
I am creating a multistep node form. The first step is to upload the a spreadsheet. Based the information in the spreadsheet, I want to pre-populate the field collection with values.
For example: (each pair is the field collection definition, which has unlimited cardinality in the form)
Field Name: One
File Type: Integer
Field Name: Two
Field Type: String
Field Name: Three
Field Type: Interger
..
..
I tried this in form_alter
$field_definition_template = $form['field_definition']['und'][0];
for($k=0;$k<count($column_names);$k++)
{
$form['field_definition']['und'][$k] = $field_definition_template;
$form['field_definition']['und'][$k]['#delta'] = $k;
}
There are two problems with this....form_alter gets called when clicking "Add another item" and there are a lot of attributes I would have to change in order to get this to actually work correctly. Is there an API or another way to do what I want to accomplish?
Comments
I'd love to know this as
I'd love to know this as well. I'm attempting the same thing.
Did you ever figure this
Did you ever figure this out?
Trying to prepopulate a multi value field_collection also.
I'm trying to work out the
I'm trying to work out the same thing - I'll post back here if I find out.
EDIT:
Did anyone found solution for
Did anyone found solution for this, I've same problem and want to pre-populate the field collection field.
Yogesh
http://yogeshchaugule.com/blog
I am also very interested.
I am also very interested. Having the same problem and no solution yet...
Finally - found a solution... (D7)
After hours of debugging I found a good working solution!
So whats the plan?
hook_form_alter()
to pre-populate values.Ok. So lets assume we have a bundle called
bundle_with_unlimited_fc
which belongs to the node entity type. Furthermore all our custom code will reside in our module namedmy_module
.We can use hook_menu_alter() to define a new page callback. Everytime a new
bundle_with_unlimited_fc
is added Drupal will call now this function.Now definde our form builder function. Notice the function argument which comes from the Drupal implementation of our altered menu item.
Thats nearly all there is. Just setup your hook_form_alter() and you will be ready.
So thats it basically. Hope that this solution will help some people to save time...
Default Value for Field within Collection
To target the actual field within the Field Collection, the final function needs to be altered:
Thanks for posting your solution SaV - real lifesaver
** EDIT
One problem I had found was an error if Field Collection Table module is used. See #1815496: Using "Hide blank items" leads to misformed tables for more info. Simplest option (for me) was to uncheck 'Hide blank items' box on Content Type field settings.
Help me,
This code is great, but I have a error when I rebuild node edit form. The fields generate OK but the "Add another item" button fails.
My code is:
And the warrings are:
I hope to help me.
I've found a different way to
I've found a different way to do this that is a bit more form-agnostic (not limited to node forms, no form rebuilding):
>
So far this works for me
So far this works for me although the last assignment statement needs to be changed thus :
$form[$field_name][LANGUAGE_NONE][$delta]['field_user_qualified_adjusted'][LANGUAGE_NONE][0]['value']['#default_value'] = '4';
I also found that to set the default value I had to target a different position in the array ( not below ['value'] ) thus :
$form[$field_name][LANGUAGE_NONE][$delta]['field_user_qualified_adjusted'][LANGUAGE_NONE][0]['#default_value'] = '4';
This will depend on the field
This will depend on the field type -- some of them prefer ['number'] instead of ['value'] and others can be set directly.
$form['field_course
where field_course_preferences is multivalue field collection and field_user_qualified_adjusted is subfield of field collection right? then field_field_name should be field_course_preferences.
Really great solution
Thanks that works for me
Thank you !
After spending hours your solution is the best
What's the trick to keep the
What's the trick to keep the 'remove' and 'add more' buttons after setting the desired amount of field collection slots?
...is not working. Thanks!
This solution worked, but i
This solution worked, but i don't need to remove "REMOVE" and "ADD ANOTHER ITEM" button . That's why i have not included following lines in my code.
The issue for me is that neither of these remove or Add item button is working.
Add another button is not working
Hi,
Specified item count is generated but 'add another item' button is not working. I try to fix and unable to add an additional item.
Kindly do the needful.
This solution works perfect
Solution posted by SaV : This solution works perfect for me BUT i don't want to use drupal_build_form as used above because using drupal_build_form will rebuild the form, hence page load time will be increased and performance of will decrease. Does any body have any other solution ??
I tried Renee's solution and
I tried Renee's solution and it seemed to work fine but when I use search and the search term is associated with a node that uses these multiple values, the sites crashes. Is anybody seeing this as well?
Are you targetting the form
Are you targetting the form correctly? This shouldn't interfere at all with any other functionality. Either that or see above re: how to set the value; it might require using a slightly different form element in the render array to set #default-value.
eta: off the top of my head, I believe you need to use ['tid'] to set taxonomy terms? I think. Anyway. That :)
This Worked for Me too... Thanks Renee :)
Instead of loading multifield
Instead of loading multifield default values again on node/edit form how to disable remove_button?
That's a different question
That's a different question to the original topic, and therefore you should start a new topic on the matter.
Add More
The suggestions for this problem work great. My only issue is that I want to keep this field as unlimited cardinality with a number of "suggestion" fields already displayed to the user when they create a new node. This means I still require the "add another item" and "remove" buttons. The problem now is that these buttons are all broken. Is there any way to pre-set the number of values I have initially and give them data while still keeping the original functionality of an unlimited field?
** EDIT
I must also say that I'm programmatically loading the form into a custom page as I need the same url for the node create and edit page. If going straight through the node create page it works fine. When in the custom page it loses access to node.pages.inc when running drupal_retrieve_form() and a mass of php errors come originally from node_form not being a valid callback. Any ideas as to why its no longer using the node functions and how I can force it to use them?
** EDIT
For anyone interested I fixed it by including node.pages.inc when field_collection/ajax is called from my hook_init().
I also followed Renee S post
I also followed Renee S post and it is populating everything as needed. But am missing "Add another item" link . I commented out
$form[$field_name][LANGUAGE_NONE][0]['remove_button']['#access'] = FALSE ;
$form[$field_name][LANGUAGE_NONE]['add_more']['#access'] = FALSE;
these lines ..But still not working... Any suggestions??
Thanks,
Kavitha
unlimited items "Remove item" problem
Function "field_multiple_value_form()" deletes "#field_parents" and "#language" from field and it breaks first usage of the "Remove item" button. This is working code from my project:
$node_wrapper are not available.
Hi maks9889,
How to Get $node_wrapper in form alter.
this code is incomplete
this code is incomplete because variables like $language and $node_wrapper are not defined and showing notice like Notice: Undefined variable: language.
when both source and
when both source and destination field collection is of same instance and machine name, should we have to loop through multivalue field collection when we have to load field collection value from already submitted node.
ex:
field_fc_test (on node preexisting data)
[0]
--fc_text1 : "some text"
--fc_text2 : "another text"
[1]
--fc_text1 : "second text"
--fc_text2 : "second another text"
If same field_fc_test is added to another bundle with same cardinality and if we need to populate on _form_alter shall we have to loop through node's field_fc_test field?
Shorter way maybe...
Here's what I did to populate a field_collection form reading the values off a product and applying it to a line item.
Allow cardinality to be restricted in the field instance setting
See: #1029298: Allow cardinality to be restricted (overriden and reduced) in the field instance settings
Yet another way to use only hook_form_alter...
I attacked this issue this way...
... and then my custom MYMODULE_after_build function...
Working solution WITH "remove/add item" buttons
Thanks to Renee and BDuel, here is a working code to pre-populate the fields AND keep the Add/Remove item functionality. Basically, you have to remove those lines from Renee's example, and let the cardinality to -1 for unlimited items
AND
rebuild the form following BDuel's example ;)
Here is a complete example :
Hope this helps ;)
thanks fakir22 alots you save
thanks fakir22 alots you save me a lot of time
this is a key why i did see anything in my form