I would like to redirect user from node edit page to node add page but with prepopulated form. So all fields of the form would be pre-filled with copies of values of node edited before. Is there a way how to get these values and fill them in the form?
3 Answers
You can try using Node Clone module...
Project page says
The module provides two mechanisms to clone current content. The default pre-populates the form for adding a new node, but nothing is saved to the DB until that form is submitted. With the alternative save/edit method, a copy of the current node is saved to the DB, and then presented in the edit form. When using the save/edit mode, the admin can set whether a confirmation screen is required, and in all cases you can set whether to reset the publishing options on a per-content-type basis.
-
Thanks. I'll have a look at this module. Anyway I would rather somehow alter the node-edit form, if possible.– hajnyonCommented Apr 9, 2014 at 23:55
The best way i can think of is to create a custom module, hook_form_alter the node-edit form and change the #default_value of the form fields/elements you care about.
So in the defualt_value attribute of this you could implement a custom function that brings the data you need or hardcode the values.
-
Thank you for your answer. Yes, this is the way I would like to do it. Can you specify the second paragraph? When I alter node-edit form as you metioned, how do I get this default_value into node-add page?– hajnyonCommented Apr 9, 2014 at 23:54
Finally I did my own way. I hooked node-edit page so it redirects user to node-add page but adds id of the node into url.
for example: when nid = 3 node-edit page after submit redirects user to node/add/3
Then I check, using current_path(), whether is there the nid after last slash in my node-add form or not. If there is no nid then user creates new node, if there is nid, user came from node-edit page and I load all node's fields by nid and prefil the form.
I was wondering if there aren't some troubles but the only problem I can think of is when I would like to use the url to do something more than just sending this nid.