Hi,
I am currently trying to migrate content from a proprietary system into a fresh Drupal 7 installation. I have enabled the Link module (http://drupal.org/project/link) and added a field of type Link to a content type.
Now I am trying to create nodes of the new type using the drupal_form_submit() method.
All works fine until it comes to the link field. I am using this code:
$form_state['values']['field_label_link']['und'][0]['url'] = 'http://www.someurl.com';
$form_state['values']['field_label_link']['und'][1]['url'] = 'http://www.anotherurl.com';
field_label_link is the name of the field in my content type, $form_state the form state variable passed into drupal_form_submit(). Apart from this I am setting the title, the body and some taxonomy terms, which all works fine.
After executing drupal_form_submit(), the node gets created, but only the first link is included. The second one does not show up.
What do I need to do to include the second link when creating the node in this way?
Thanks for any help,
best regards,
Arngrim
Comments
As expected, the issue is
As expected, the issue is correlated to the fact that the Link field (like other fields allowing an unlimited number of values) creates new form fields upon request using Ajax. This process does not happen when using the drupal_form_submit method with a two-dimensional array for the Link data.
As a workaround I have limited the number of allowed values for my input type so that all form fields get generated when first calling the dialog - hence the above notation works when trying to add multiple links to a new node.
Without having dug too deep into the internals I assume that the form only processes values for which input fields are available, so without the ajax request in the original form (which creates a second (third, etc) field for the Link field) only one field exists and thus the other submitted values are ignored.
In order to get an answer to my original question, the question therefore needs to be rephrased: how to programmatically update the form to include more than one link field, i.e. simulate the ajax request when creating a new node this way?
Solution
I hope you sorted this out, just replying as this was the first result on google when I got stuck.
You can't use drupal_form_submit directly, you have to copy the code from inside drupal_form_submit and then manipulate the form_state before processing. Remembering to set $form_id and $form_state['build_info']['args'] manually, you need:
Then the following, taken from field_add_more_submit:
Then the rest of the drupal_form_submit code.
Hope this helps.
Steve
Thanks a lot
Thanks a lot steve, your explanation was very useful ... saved me tons of time.
Cheers!
S.
Same solution -- works for all fields automatically.
I have lots of custom fields and I didn't want to hard code my fieldnames "YOUR_FIELD_NAME" etc... I needed this to work across the board.
The below code is the "Extra" (middle) code siharris added in siharris's copy of drupal_form_submit(). I automatically add fields based on the form_values.
Works good for me.
Alternate solution
You can still call drupal_form_submit() by using this technique:
1) setup your $form_values with multiple values
2) call drupal_form_submit() with the form_id of a custom function:
for content type called 'mynode'
This solution is perfect for
This solution is perfect for my situation as well. Thanks!
Dennis
oh-soyummy.com
Thanks, this works fine.
Thanks, this works fine.
Heshan Wanigasooriya
Github
This solution doesn't work
This solution doesn't work for me.
I have Commerce Product entity in which I have field collection.
After performing some action, When user redirects to Add Product form, based on some predefined values, I want to display User a Field collection pre-filled.
If I have 5 predefined values then 5 Field-collection items would be displayed to User and by clicking on save that product would be saved with that field collection item values.
By clicking on 'Add another item', User can add more as well. Above field collection items won't be saved until User clicks on Save form button.
May be I am doing something wrong.
I tried below code :
drupal_rebuild_form() is not working for me in Add Product form load. It goes into infinite loop.
Please help me here. How can I resolve the issue ?
Maybe this code can help
Maybe this code can help someone
Fredrik Lahode
Développeur et Formateur web indépendant
It did help someone :-)
Probably it helped more people along the way, but I wanted to say that, almost 10 years after this comment was made, it did help someone... me!
Thanks so much.