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.
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 thatcontact_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 useset
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:
contact_email
component, (not modifying the node vessel). I'd suggest you look at the code under the example functionhook_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 ofcontact_email
)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.I hope this helps....