diff --git a/modules/webform_submission_log/src/WebformSubmissionLogLogger.php b/modules/webform_submission_log/src/WebformSubmissionLogLogger.php index 0aa0cd3..1b99098 100644 --- a/modules/webform_submission_log/src/WebformSubmissionLogLogger.php +++ b/modules/webform_submission_log/src/WebformSubmissionLogLogger.php @@ -66,7 +66,7 @@ class WebformSubmissionLogLogger implements LoggerInterface { $context += [ 'handler_id' => '', 'operation' => '', - 'data' => [], + 'data' => $webform_submission->logData(), ]; // Cast message to string. diff --git a/src/Entity/WebformSubmission.php b/src/Entity/WebformSubmission.php index 1583279..9f1c20f 100644 --- a/src/Entity/WebformSubmission.php +++ b/src/Entity/WebformSubmission.php @@ -498,6 +498,41 @@ class WebformSubmission extends ContentEntityBase implements WebformSubmissionIn return $this->dataHash; } + /** + * Returns data which can be used for logging an event. + * + * For example, if a webform submission has been modified, the data is + * what has changed. + * + * @return array + * Associative array with "is-new" (boolean) and "changed", itself an + * associative array of field keys with their before and after + * values, if possible. + */ + public function logData() { + $return = [ + 'is-new' => $this->isNew(), + 'changed' => [], + ]; + if ($return['is-new']) { + return $return; + } + $original = $this->getOriginalData(); + $data = $this->getData(); + foreach ($data as $key => $value) { + if (isset($original[$key]) && $original[$key] == $value) { + continue; + } + else { + $return['changed'][$key] = [ + 'from' => empty($original[$key]) ? '' : $original[$key], + 'to' => $value, + ]; + } + } + return $return; + } + /** * {@inheritdoc} */ diff --git a/src/WebformSubmissionStorage.php b/src/WebformSubmissionStorage.php index 45e2367..591790b 100644 --- a/src/WebformSubmissionStorage.php +++ b/src/WebformSubmissionStorage.php @@ -1031,6 +1031,7 @@ class WebformSubmissionStorage extends SqlContentEntityStorage implements Webfor '@title' => $entity->label(), 'link' => ($entity->id()) ? $entity->toLink($this->t('Edit'), 'edit-form')->toString() : NULL, 'webform_submission' => $entity, + 'data' => $entity->logData(), ]; switch ($entity->getState()) { case WebformSubmissionInterface::STATE_DRAFT_UPDATED: @@ -1110,6 +1111,7 @@ class WebformSubmissionStorage extends SqlContentEntityStorage implements Webfor '@id' => $entity->id(), '@title' => $entity->label(), 'link' => ($entity->id()) ? $entity->toLink($this->t('Edit'), 'edit-form')->toString() : NULL, + 'data' => $entity->logData(), ]; $this->loggerFactory->get('webform')->notice($message, $context); }