- Issue created by @Monster971
- First commit to issue fork.
- Status changed to Needs review
over 1 year ago 6:22am 24 March 2023 - 🇮🇳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(); } }
- Status changed to Postponed: needs info
over 1 year ago 8:04am 24 March 2023 - 🇬🇵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 6:43pm 28 March 2023 - 🇬🇵Guadeloupe Monster971
Thanks for the answer, but it still doesn't work, I think the ->save() method will always overwrite the update date.