I am using the Field Timer module to display countdown timer on my article node.
This module will take the value of the date/time field (my case: field_article_datetime) and convert it into a countdown timer in the display of the article.
I have a webform (machine name: ABC) attached as a field to the article content type so I can use [webform_submission:node:nid]
to retrieve the article nid of the article to be updated.
The webform (machine name: ABC) has below element:
1) Select element (machine name: increase_time_in_minutes):
- Text: Increase by 15 minutes (Value: 15)
- Text: Increase by 30 minutes (Value: 30)
- Text: Increase by 45 minutes (Value: 45)
And the article content type has below field:
- date/time field (machine name: field_article_datetime) (the Field Timer module is applied to this field)
When the webform "ABC" is submitted, I want to:
- Fetch the article nid (
[webform_submission:node:nid]
) - Set the value of the date/time field (field_article_datetime) equal to current time + increase_time_in_minutes
EXAMPLE:
Say at 14/03/2019 08:30:15 PM, a user has submitted the webform "ABC" and selected Increase by 15 minutes option from the select list... So I need the related article to be fetched and update the (field_article_datetime) to have the value of 14/03/2019 08:45:15 PM
This way, the user will see a countdown timer for 15 minutes on the article node display page.
I am not a programmer, but I think the best Idea is to use a customwebformhandler.php file which will be enabled for the webform settings page.
I have created the file but I need help to finish it:
<?php
namespace Drupal\my_module_extras\Plugin\WebformHandler;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Serialization\Yaml;
use Drupal\Core\Form\FormStateInterface;
use Drupal\node\Entity\Node;
use Drupal\webform\WebformInterface;
use Drupal\webform\Plugin\WebformHandlerBase;
use Drupal\webform\webformSubmissionInterface;
use Drupal\webform\Entity\WebformSubmission;
/**
* Update a node entity from a webform submission.
*
* @WebformHandler(
* id = "Update a node",
* label = @Translation("Update a node"),
* category = @Translation("Entity Update"),
* description = @Translation("Updates a node from Webform Submissions."),
* cardinality = \Drupal\webform\Plugin\WebformHandlerInterface::CARDINALITY_UNLIMITED,
* results = \Drupal\webform\Plugin\WebformHandlerInterface::RESULTS_PROCESSED,
* submission = \Drupal\webform\Plugin\WebformHandlerInterface::SUBMISSION_REQUIRED,
* )
*/
class SetTimerOnNodeWebformHandler extends WebformHandlerBase {
/**
* {@inheritdoc}
*/
// Function to be fired while submitting the Webform.
public function submitForm(array &$form, FormStateInterface $form_state, WebformSubmissionInterface $webform_submission) {
// Get an array of the values from the submission.
$values = $webform_submission->getData();
// Fetch & Update node object from webform-submission.
//get current node or use any other way to get the current node
$node = \Drupal::routeMatch()->getParameter('node');
if ($node instanceof \Drupal\node\NodeInterface) {
$current_time = \Drupal::time()->getCurrentTime();
date_default_timezone_set('GMT');
//get value from webform
$increase = $values["increase_time_in_minutes"];
//calculate new time
$timer = date('Y-m-d\TH:i:s', $current_time + $increase * 60);
//add it to article field and save
$node->field_article_datetime->value = $timer;
$node->save();
}
}
}
Update #2:
The below error is generated because of one missing }
at the end of the file. (Stupid mistake)
Update #1: Error logged in in the dblog after trying to add the webformhandler.php file to the webform:
ParseError: syntax error, unexpected end of file, expecting function (T_FUNCTION) or const (T_CONST) in Composer\Autoload\includeFile() (line 59 of S:\######\dev.####\web\modules\custom\my_module_extras\src\Plugin\WebformHandler\SetTimerOnNodeWebformHandler.php) #0 S:\######\dev.#####\vendor\composer\ClassLoader.php(322): Composer\Autoload\includeFile('S:\\###### ...') #1 [internal function]: Composer\Autoload\ClassLoader->loadClass('Drupal\\my_module...') #2 [internal function]: spl_autoload_call('Drupal\\my_module...') #3 S:\#######\dev.#####\web\core\lib\Drupal\Component\Plugin\Factory\DefaultFactory.php(96): class_exists('Drupal\\my_module...') #4 S:\#######\dev.#####\web\core\lib\Drupal\Core\Plugin\Factory\ContainerFactory.php(17): Drupal\Component\Plugin\Factory\DefaultFactory::getPluginClass('Update a node', Array, 'Drupal\\webform\\...')
attached as a field to the article content type
. How is this done ? Do you mean include as a block under article content type ?