Set a date field equal to “current time” + “5 minutes” using tokens
Question
I am using the Field Timer module to display countdown timer on my article node.
Say my article node has the date/time field named (field_article_datetime)
This module will take the value of the date/time field (field_article_datetime) and convert it into a countdown timer in the display of the article.
When the article node is created, I want to set the value of the date/time field (field_article_datetime) equal to current time + 5 minutes.
To extract the current time, I am using the token [node:created] from the token module, but I cannot find a solution on how to add +5 minutes to the token [node:created].
EXAMPLE:
Say at 12/03/2019 08:50:15 PM, a user has created a new article node... So I need a solution to add +5 minutes to the created time so the date/time field named (field_article_datetime) will be equal to 12/03/2019 08:55:15 PM...
This way, the user will see a countdown timer for 5 minutes on the article node display page.
Note: I am using the Business Rules module (similar to Rules module) to set the value of the date/time field (field_article_datetime) with the help of the token module.
Update #1: Say the node is already created on the site and I want after the submission of a webform to fetch & update the node with update time + 5 minutes.
I think the best Idea is to use a custom module which will do the below:
- Fetch the article by nid.
- Set the value of
field_article_datetime
equal to:updated time + 5 * 60
. - Save the Node
Since the above actions will be triggered when a user will submit a webform, so why not create a customwebformhandler.php file.
I have created a custom webform handler file but I need help to finish it since I am not a programmer:
<?php
namespace Drupal\my_module_name\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 UpdateNodeWebformHandler extends WebformHandlerBase {
/**
* {@inheritdoc}
*/
// Fetch & Update node object from webform-submission.
// 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();
// The webform has an element text field with machine name (update_nid) which
// contains the node ID of the node to be updated.
// This is the part where to fetch the node by the value of (update_nid) element.
Some code here
// This is the node Updating part.
$created = $entity->updated->value;
date_default_timezone_set('GMT');
$createdadd5 = date('Y-m-d\TH:i:s', $created + 5 * 60);
$entity->field_article_datetime->value = $createdadd5;
// This is the node Saving part.
$node->save();
}
}
Solution
I can't think of a solution with just tokens. Here is a quick and dirty solution without tokens
Setup your Article datetime field so that has a default relative time of +5 minutes
You can hide this field with css so the user will not change it or you can do it in a hook_form_alter
function in your MODULENAME.module
file
// adjust node_article_form for your content type
if($form_id == 'node_article_form' || $form_id == "node_article_edit_form"){
$form["field_article_datetime"]['widget'][0]['value']['#attributes']['readonly'] = 'readonly';
}
Of course if a user takes much time to fill the form the set datetime might not reflect exactly the NODE CREATION TIME +5 minutes (as a datetime is set on form creation time)
If the above is not good enough you can probably use hook_entity_presave
to adjust the time to be EXACTLY the creation time +5 minutes.
function MODULENAME_entity_presave(Drupal\Core\Entity\EntityInterface $entity) {
// adjust article to reflect your content type
if ($entity->bundle() == "article" && $entity->isNew()) {
$created = $entity->created->value;
date_default_timezone_set('GMT');
$createdadd5 = date('Y-m-d\TH:i:s', $created + 5 * 60);
$entity->field_article_datetime->value = $createdadd5;
}
}
NOTE: if you want to update the timer even on node update you can just delete the && $entity->isNew()
part.