tags:

views:

23

answers:

2

I have a Drupal view that displays vacation leaves for a particular employee.

I want to have a button or link right on this view that says 'Add Vacation Leave'. When this button/link is clicked, I want the create content form for Vacation Leave to be displayed with the employee's name already filled in.

When 'Save' is clicked, I would like to go right back to the view, with my new content added. Is all of this possible?

Thanks, Paul

+1  A: 

Sure. If I am reading this right, you can write a simple module that implements hook_form_alter, then prefill the form field by doing:

global $user;
$form['name_of_form_field']['#value'] = $user->name;

You can also adjust the redirect within the same function.

There is also a module that can auto populate based on URL variables.

http://drupal.org/project/prepopulate

Kevin
I tried Prepopulate, but it didn't work at all for me - I provide the syntax just like the documentation says, but nothing is changed on my form. Perhaps I need to take this up with the module's authors.
pduncan
+1  A: 

You can do this without much trouble.

To add the link, you could make a custom template for the view where you add the link. It will be the same for all users, something like, node/node_type/add. You should make the link with the l() function, and remember to add the destination to where they should be redirected back to. You should get a url like this: /node/node_type/add&destination=/url/to/current/page (the / being escaped ofcause).

With destination being set, the default node form, should take care of everything and redirect the user back to the url he came from.

googletorp
Thanks googletorp - this works great! Now I just have to figure out the passing arguments and filling in the form thing...
pduncan