user1425041
user1425041

Reputation: 1

Drupal - Alter webform field before it is rendered

I am VERY new to Drupal (Drupal 7).

I am stuck at webform alteration step.

So, I have created a content type - News.

The News content type has a node reference field for referencing a webform.

Created a view (for various reasons) to list the news items.

Now, when the news item details page is displayed, I want to update a couple of webform fields. For example, the webform subject's field needs to be pre-populated with the News item title.

I understand that there is hook_form_alter hook to update the webform, but I can't use that hook as I won't have access to the other News content type fields.

I tried template_preprocess_node hook, but looks like that is called after the webform has been rendered as the field values were not updated.

Any help would be much appreciated.

Upvotes: 0

Views: 1888

Answers (2)

mediaashley
mediaashley

Reputation: 352

I know this is an old post, but for anyone searching for answers.

  1. A webform doesn't need to be referenced in, it can be attached to your content type (in this case News). Although it looks like @user1425041 was creating some kind of feedback form, so probably wanted to reuse one form for several nodes.
  2. As you are wanting to alter the webfrom from the News node, you do have access to the full node and all the fields in content type via menu_get_object(), however you don't need to reload the node via node_load as menu_get_object() will give you the full node object.
  3. Assuming you have the form_id you can then use hook_form_alter or hook_form_<formid>_alter() to alter the contents of the form.

More info can be found here: https://www.drupal.org/node/1558246

Upvotes: 1

pete80
pete80

Reputation: 545

Is the item detail page a node page if so you can use menu_get_object(); to get the nid then use node_load to get the data you require and then hook_form_alter to insert it into the webform.

If it is not a node page you can use devel or hook_page_alter to look for a reference to the node in the page and then use this to load the data from the node and insert into your form.

Upvotes: 0

Related Questions