1

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:

  1. Fetch the article by nid.
  2. Set the value of field_article_datetime equal to: updated time + 5 * 60.
  3. 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();
  }
}
1

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

enter image description here

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.

  • Your first solution is perfect but as you said is not good enough bcz the default time is set only on form creation time. I might have a workaround solution but I need your coding skills since mine are so limited! Please refere to the update #1 of my question. Thank you – Elie Masaad Mar 13 at 17:48
  • The is a second part that overcomes the first problem. Did you try it ? Perhaps you missed something ? – GiorgosK Mar 13 at 18:26
  • If you refer to the customwebformhandler code posted in the question, you will see that I left empty the part of fetching the node because I didn't know how to do it... In addition, I am not 100% sure if the part related to updating the entity is is correct or not... Can you please check my above code ? – Elie Masaad Mar 13 at 18:40
  • I forget to mention in my question I need also to update the field_article_datetime to be equal to updated time + 5 minutes every time the article node is updated... Can you alter the code posted in your above answer to fit my requirement here ? – Elie Masaad Mar 13 at 19:32
  • 1
    @ElieMasaad my solution is also complete and tested, I have added a NOTE for what to do when the node is updated (which is not mentioned in your question original or update1). Webform involvement was not in the original question either and still its not clear how it relates to article content type. It seems you have added details to the question that change the approach to the solution. Please try the proposed solution instead of asking for a change in direction of the solution. – GiorgosK Mar 13 at 21:53

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.