drupal 7, i have form in template as so print render(node_view(node_load(478), 'full', NULL)['webform']);
i need to check if form is sended ok, in the same template with php, to show ok message. how to do this?
Assistant
In Drupal 7, if you are rendering a webform within a template and want to check if the form has been successfully submitted, you would typically handle this logic within a custom module or potentially with some pre-processing in your template file. However, it's important to note that directly handling logic in template files is generally not best practice. But, since you're asking for a way to display a confirmation message after form submission directly in the template, here's how you could do it:
Create a Preprocessor Function: You should create a theme preprocessor function in your template.php file. This function can check if the webform has been submitted and pass a variable to your template.
Use Webform Hooks: You can use webform-specific hooks such as hook_form_FORM_ID_alter() to determine if a form has been submitted.
Here's an approach using the preprocessor function:
php
/**
* Implements hook_preprocess_HOOK() for page templates.
*/
function yourtheme_preprocess_page(&$variables) {
// Check if you are on the page of the node that contains the webform.
if (arg(0) == 'node' && arg(1) == 478) {
// Load the webform node.
$node = node_load(478);
if ($node && $node->type == 'webform') {
// Check if the webform has been submitted.
if (module_exists('webform') && webform_submission_confirm($node->nid)) {
// Add a confirmation message variable to the template variables.
$variables['webform_confirmation'] = t('Thank you for your submission.');
}
}
}
}
Update the Template: In your template file, check if the webform_confirmation variable is set, and if so, print the confirmation message.
Here's an example of what your template might look like:
webform_submission_confirm(): This function checks if there is an active submission for the specified node ID (nid). It's a convenient way to verify if the form was submitted.
Template Preprocessor: The preprocessor function allows you to prepare variables and logic outside the template file, promoting better separation of logic and presentation.
yourtheme: Replace yourtheme with the actual name of your theme.
Remember, it's usually a good idea to handle form submission logic within custom modules or using Drupal hooks, where possible, for cleaner and more maintainable code.