Maggie
Maggie

Reputation: 33

Drupal 8: When I update the node field to a specific value, how to call my module (managefinishdate.module) to update another field?

I am having a node type with machine name to_do_item, and I want to create a module called managefinishdate to update the node: when a user edit the node's field (field_status) to "completed" and click "save", then the module will auto update the field_date_finished to current date.

I have tried to create the module and already success to install in "Extend": function managefinishdate_todolist_update(\Drupal\Core\Entity\EntityInterface $entity) { ... } however, I am not sure is this module being called, because whatever I echo inside, seems nothing appeared.

<?php

use Drupal\Core\Entity\EntityInterface;
use Drupal\node\Entity\Node;

/**  * Implements hook_ENTITY_TYPE_update().
* If a user update status to Completed,
* update the finished date as save date
*/

function managefinishdate_todolist_update(\Drupal\Core\Entity\EntityInterface $entity) {
    $node = \Drupal::routeMatch()->getParameter('node');
    print_r($node);

  //$entity_type = 'node';
  //$bundles = ['to_do_item'];

  //$fld_finishdate = 'field_date_finished';
  //if ($entity->getEntityTypeId() != $entity_type || !in_array($entity->bundle(), $bundles)) {
    //return;
  //}
  //$current_date=date("Y-m-d");
  //$entity->{$fld_finishdate}->setValue($current_date);

} 

Upvotes: 0

Views: 1412

Answers (2)

briangonzalezmia
briangonzalezmia

Reputation: 86

Following Drupal convention, your module named 'manage_finish_date' should contain a 'manage_finish_date.module file that sits in the root directory that should look like this:

<?php

use Drupal\node\Entity\Node;
/**
 * Implements hook_ENTITY_TYPE_insert().
 */
function manage_finish_date_node_insert(Node $node) {
  ManageFinishDate::update_time();
}
/**
 * Implements hook_ENTITY_TYPE_update().
 */
function manage_finish_date_node_update(Node $node) {
  ManageFinishDate::update_time();
}

You should also have another file called 'src/ManageFinishDate.php' that should look like this:

<?php

namespace Drupal\manage_finish_date;

use Drupal;
use Drupal\node\Entity\Node;

class ManageFinishDate {

  public static function update_time($node, $action = 'create') {

    // Entity bundles to act on
    $bundles = array('to_do_item');
    if (in_array($node->bundle(), $bundles)) {
      // Load the node and update
      $status = $node->field_status->value;
      $node_to_update = Node::load($node);
      if ($status == 'completed') {
        $request_time = Drupal::time();
        $node_to_update->set('field_date_finished', $request_time);
        $node_to_update->save();
      }
    }
  }
}

The code is untested, but should work. Make sure that the module name, and namespace match as well as the class filename and class name match for it to work. Also, clear you cache once uploaded.

This will handle newly created and updated nodes alike.

Upvotes: 2

Vipin Mittal
Vipin Mittal

Reputation: 388

Please look after this sample code which may help you:

function YOUR_MODULE_entity_presave(Drupal\Core\Entity\EntityInterface $entity) {

switch ($entity->bundle()) {

//Replace CONTENT_TYPE with your actual content type

case 'CONTENT_TYPE':

$node = \Drupal::routeMatch()->getParameter('node');

if ($node instanceof \Drupal\node\NodeInterface) {

// Set the current date

}

break;

}

}

Upvotes: 0

Related Questions