0

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):

  1. Text: Increase by 15 minutes (Value: 15)
  2. Text: Increase by 30 minutes (Value: 30)
  3. Text: Increase by 45 minutes (Value: 45)

And the article content type has below field:

  1. 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:

  1. Fetch the article nid ([webform_submission:node:nid])
  2. 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\\...')
  • 1
    Please elaborate on attached as a field to the article content type. How is this done ? Do you mean include as a block under article content type ? – GiorgosK Mar 14 at 7:18
  • 1
    I have added a new field of type webform and select the webform ABC from the select droodown list list of all available webforms. – Elie Masaad Mar 14 at 11:48
  • 1
    you are right, I had never noticed that you could include webform as a field. The solution I posted should work for this case as well. – GiorgosK Mar 14 at 12:51
2

Use this as your submitForm

$values = $webform_submission->getData();

//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();
}    

The answer asumes that you have included the webform as a block for article content types. Another possibility is to include the webform as field and it should work exactly the same.

NOTE: before the webform handler works you have to enable your custom module and than go to your webform settings > email/handlers and include your handler

  • I have updated the webformhandler.php file with the code in your answer and successfully enabled the module... However, I cannot add the handler to the webform with an error logged in dblog... See the Update #1 of my question please – Elie Masaad Mar 14 at 13:11
  • 1
    You are missing a closing brace } of the class and also make sure that your file is placed in MODULENAME/src/Plugin/WebformHandler/SetTimerOnNodeWebformHandler.php – GiorgosK Mar 14 at 13:34
  • 1
    The drupal community is the best because of people just like you who are willing to help at any time. Thank you @GiorgosK you rock! – Elie Masaad Mar 14 at 14:41
  • For other users who need a complete solution for similar issue, just copy the entire code available in my question above and paste it in your webformhandler.php file – Elie Masaad Mar 14 at 14:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.