setChangedTime() are not working at all

Created on 23 March 2023, over 1 year ago
Updated 28 March 2023, over 1 year ago

Problem/Motivation

Hello,

I'm trying to update my webform submissions via cron without changing the changedTime date.

For that I use the ->setChangedTime($timestamp) method which seems to be overwritten by the ->save() method.

Here is my code:

function mymodule_cron()
{
  $query = \Drupal::entityQuery('webform_submission')
    ->condition('webform_id', 'mywebform')
    ->sort('created', 'ASC')
    ->accessCheck(FALSE);
  $result = $query->execute();
  $storage = \Drupal::entityTypeManager()->getStorage('webform_submission');
  $submissions = $storage->loadMultiple($result);
  
  foreach ($submissions as $submission) {
    
    $originalChangedTime = $submission->getChangedTime();
    $user_id = $submission->getOwnerId();
    $user = \Drupal\user\Entity\User::load($user_id);
    
    // Récupération des champs utilisateurs.
    $sp = $user->field_situation_p->value;
    $sf = $user->field_situation_f->value;
    $cua = $user->field_cua->target_id;
    
    if (!empty($sp)) {
      $submission->setElementData('sp', $sp);
    }
    
    if (!empty($sf)) {
      $submission->setElementData('sf', $sf);
    }
    if (!empty($cua)) {
        $submission->setElementData('cua', $cua);
    }
    $submission->setChangedTime($originalChangedTime);
    $submission->save();
  }
}

While we cannot update the fields without using the ->save() method. How to solve this problem?

💬 Support request
Status

Closed: won't fix

Version

6.1

Component

Code

Created by

🇬🇵Guadeloupe Monster971

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @Monster971
  • First commit to issue fork.
  • Status changed to Needs review over 1 year ago
  • 🇮🇳India dharmeshbarot Gujarat
    function mymodule_cron() {
      $query = \Drupal::entityQuery('webform_submission')
        ->condition('webform_id', 'mywebform')
        ->sort('created', 'ASC')
        ->accessCheck(FALSE);
      $result = $query->execute();
      $storage = \Drupal::entityTypeManager()->getStorage('webform_submission');
      $submissions = $storage->loadMultiple($result);
      
      foreach ($submissions as $submission) {
        $user_id = $submission->getOwnerId();
        $user = \Drupal\user\Entity\User::load($user_id);
        
        // Récupération des champs utilisateurs.
        $sp = $user->field_situation_p->value;
        $sf = $user->field_situation_f->value;
        $cua = $user->field_cua->target_id;
        
        if (!empty($sp)) {
          $submission->set('elements.sp', $sp);
        }
        
        if (!empty($sf)) {
          $submission->set('elements.sf', $sf);
        }
        
        if (!empty($cua)) {
          $submission->set('elements.cua', $cua);
        }
        
        $submission->save();
      }
    }
    
    
  • 🇮🇳India dharmeshbarot Gujarat

    Try this if it's work.

  • Status changed to Postponed: needs info over 1 year ago
  • 🇬🇵Guadeloupe Monster971

    Hello @dharmeshbarot,

    Thanks for the answer but the changed date is updated by the current timestamp.

    Whereas if my changed date is 03/24/2023 - 6:00 am, I want after the cron run that the date remains 03/24/2023 - 6:00 am, not the date where the cron puts update on the webform submission..

  • 🇮🇳India dharmeshbarot Gujarat
    function mymodule_cron() {
      $query = \Drupal::entityQuery('webform_submission')
        ->condition('webform_id', 'mywebform')
        ->sort('created', 'ASC')
        ->accessCheck(FALSE);
      $result = $query->execute();
      $storage = \Drupal::entityTypeManager()->getStorage('webform_submission');
      $submissions = $storage->loadMultiple($result);
      
      foreach ($submissions as $submission) {
        $user_id = $submission->getOwnerId();
        $user = \Drupal\user\Entity\User::load($user_id);
        
        // Récupération des champs utilisateurs.
        $sp = $user->field_situation_p->value;
        $sf = $user->field_situation_f->value;
        $cua = $user->field_cua->target_id;
    
        // Store the original changed timestamp before modifying the submission data.
        $originalChangedTime = $submission->getChangedTime();
    
        if (!empty($sp)) {
          $submission->setElementData('sp', $sp);
        }
        
        if (!empty($sf)) {
          $submission->setElementData('sf', $sf);
        }
        if (!empty($cua)) {
          $submission->setElementData('cua', $cua);
        }
    
        // Restore the original changed timestamp before saving the submission.
        $submission->setChangedTime($originalChangedTime);
        $submission->save();
      }
    }
    
    
  • 🇬🇵Guadeloupe Monster971

    Thank you @dharmeshbarot, I'm waiting for your answer

  • 🇮🇳India dharmeshbarot Gujarat
    function mymodule_cron() {
      $query = \Drupal::entityQuery('webform_submission')
        ->condition('webform_id', 'mywebform')
        ->sort('created', 'ASC')
        ->accessCheck(FALSE);
      $result = $query->execute();
      $storage = \Drupal::entityTypeManager()->getStorage('webform_submission');
      $submissions = $storage->loadMultiple($result);
      
      foreach ($submissions as $submission) {
        $user_id = $submission->getOwnerId();
        $user = \Drupal\user\Entity\User::load($user_id);
        
        // Récupération des champs utilisateurs.
        $sp = $user->field_situation_p->value;
        $sf = $user->field_situation_f->value;
        $cua = $user->field_cua->target_id;
    
        // Load the full submission entity object to be able to modify its fields.
        $full_submission = $storage->load($submission->id());
    
        if (!empty($sp)) {
          $full_submission->setElementData('sp', $sp);
        }
        
        if (!empty($sf)) {
          $full_submission->setElementData('sf', $sf);
        }
        if (!empty($cua)) {
          $full_submission->setElementData('cua', $cua);
        }
    
        // Preserve the original changed timestamp by explicitly setting it again.
        $full_submission->set('changed', $submission->getChangedTime());
    
        // Call save() to persist the changes to the submission entity.
        $full_submission->save();
      }
    }
    
    
  • Status changed to Closed: won't fix over 1 year ago
  • 🇬🇵Guadeloupe Monster971

    Thanks for the answer, but it still doesn't work, I think the ->save() method will always overwrite the update date.

Production build 0.71.5 2024