Problem/Motivation

Developers need an easy way to get and set (custom) submission data.

Proposed resolution

Add set/get submission element value methods

API changes

  • WebformSubmission::setElementData($key, $value);
  • WebformSubmission::getElementData($key);
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

jrockowitz created an issue. See original summary.

  • jrockowitz committed 9f9969d on 2912197-get-set-element-data
    Issue #2912197: Add set/get submission element value methods.
    
jrockowitz’s picture

Status: Active » Needs review
FileSize
21.52 KB

Status: Needs review » Needs work

The last submitted patch, 3: add_set_get_submission-2912197-2.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

  • jrockowitz committed 3c50390 on 2912197-get-set-element-data
    Issue #2912197: Add set/get submission element value methods.
    
jrockowitz’s picture

Status: Needs work » Needs review
jrockowitz’s picture

  • jrockowitz committed c1cf742 on 8.x-5.x
    Issue #2912197 by jrockowitz: Add set/get submission element value...
jrockowitz’s picture

Status: Needs review » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

Sam152’s picture

+++ b/src/Entity/WebformSubmission.php
@@ -340,13 +340,26 @@ class WebformSubmission extends ContentEntityBase implements WebformSubmissionIn
-  public function getData($key = NULL) {
-    if ($key !== NULL) {
-      return (isset($this->data[$key])) ? $this->data[$key] : NULL;
-    }
-    else {
-      return $this->data;
+  public function getElementData($key) {
+    return (isset($this->data[$key])) ? $this->data[$key] : NULL;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function setElementData($key, $value) {
+    // Make sure the element exists before setting its value.
+    if ($this->getWebform()->getElement($key)) {
+      $this->data[$key] = $value;
     }
+    return $this;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function getData() {
+    return $this->data;
   }

Hm, this was a bit of a BC break in the last update of webform I did on a custom project. Is BC being considered during the beta period?