Hello there, I am trying to create a webform that takes as a query parameter someone's full name separated by a space. Once it is submitted I want it to email that person, with their email address defined as their first initial plus their last name @site.com. I made a custom module and the code is below:

 

<?php
function webform_email_generate_webform_submission_presave(Node $node) {
	$full_name = $_GET['name'] ?? '';
	
	//I have a watchdog here that reports what $full_name is and it works perfectly
	if(!empty($full_name)) {
		$parts = explode(' ', trim($full_name));
		if(count($parts) >= 2) {
			$first = strtolower($parts[0]);
			$last = strtolower(end($parts));
			$email = substr($first, 0, 1) . $last . '@site.com';
			
			$node->set('contact_email', $email);
			$node->save();
		}	
	}
}

 

However the lines $node->set('contact_email', $email); (and presumably also $node->save();) are giving me issues, as I gather that is not the way to change the field like that. I get the error "Error: Call to undefined method Node::set()". Also, if anyone has links to documentation where I can read about this, that would also be helpful. Thank you for reading.

 

Accepted answer

Hi Colbycat. 

There are a lot of unclear, potentially problematic, aspects in your function. To start with, the first parameter received by the hook implementation you pasted (hook_webform_submission_presave) is the node where the webform "lives" - so, this is a unique, sole node that "contains" the webform. This node has nothing to do with submitted values - it doesn't store any of the webform submitted values - it just acts as a "vessel" to the webform so that it can be displayed.  And I assume that contact_email is actually not a field that belongs to the node "vessel" itself, but rather, a component that belongs to the webform referenced in the node.

Second problem, there is no method set in the Node class, so you can't really use set there. 

And third problem, you are saving the node (which probably does not contain any fields other than the webform reference itself), rather than saving the modified component in the webform submission. 

So, for this to work you need to dig a bit and figure out:

  1. How to modify the webform submission itself to set the value of the contact_email component, (not modifying the node vessel). I'd suggest you look at the code under the example function hook_webform_submission_presave, which provides an example of modifying the value of one of the components (BTW, components in the submission are referenced by component ID, not by machine name, so you need to find out the component ID of contact_email)
  2. You do not need to do anything to save the value, since the presave hook is invoked before the webform submission is saved. So, forget node_save(). If you are setting the values correctly, webform core will save the submission with your new value for you - no need to save anything.
  3. I'm not sure how you intend to send an email - I assume you'll use the email functionality of webform? If that's the case, setting the component will do the trick if you set up the functionality correctly.

I hope this helps....

Most helpful answers

Component ID can be found here: 

Comments

Hi Colbycat. 

There are a lot of unclear, potentially problematic, aspects in your function. To start with, the first parameter received by the hook implementation you pasted (hook_webform_submission_presave) is the node where the webform "lives" - so, this is a unique, sole node that "contains" the webform. This node has nothing to do with submitted values - it doesn't store any of the webform submitted values - it just acts as a "vessel" to the webform so that it can be displayed.  And I assume that contact_email is actually not a field that belongs to the node "vessel" itself, but rather, a component that belongs to the webform referenced in the node.

Second problem, there is no method set in the Node class, so you can't really use set there. 

And third problem, you are saving the node (which probably does not contain any fields other than the webform reference itself), rather than saving the modified component in the webform submission. 

So, for this to work you need to dig a bit and figure out:

  1. How to modify the webform submission itself to set the value of the contact_email component, (not modifying the node vessel). I'd suggest you look at the code under the example function hook_webform_submission_presave, which provides an example of modifying the value of one of the components (BTW, components in the submission are referenced by component ID, not by machine name, so you need to find out the component ID of contact_email)
  2. You do not need to do anything to save the value, since the presave hook is invoked before the webform submission is saved. So, forget node_save(). If you are setting the values correctly, webform core will save the submission with your new value for you - no need to save anything.
  3. I'm not sure how you intend to send an email - I assume you'll use the email functionality of webform? If that's the case, setting the component will do the trick if you set up the functionality correctly.

I hope this helps....

Hey there, and thank you for the response. As for sending the email, yes, I am using the email functionality of webform. I was wondering where I could find the component ID of the field? Or where I could find that information, as I looked through the docs and couldn't find anything. Thanks.

Component ID can be found here: