0

I'm looking to access the values of the fields in postSave().

public function postSave(WebformSubmissionInterface $webform_submission, $update = TRUE) {
  //what's in $webform_submission? can I use it to get field data that was saved/submitted?
}

How do I get which fields are available from $webform_submission? How do I access them?

I have a field whose machine name is *feedback_type. I need to access its properties from that function.

UPDATE

So essentially what I'm wondering is, is there a way to see the structure of the $webform_submission variable? so that I can retrieve the data that the user submitted? I'd prefer to be able to do a print of it whether that's to a file/log/etc. doesn't matter I'd just like to know the structure of the $message_submission and its fields.

0

Might depend a little on what you're trying to do with those fields (get info/set info/adjust info), but here are a couple of things you could try:

CustomHandler.php

For submission data:

/**
 * {@inheritdoc}
 */
public function postSave(WebformSubmissionInterface $webform_submission, $update = TRUE) {

  // Get the submitted webform data.
  $data = $webform_submission->getData();

  // Get specific submission value.
  $myFieldValue = $data['field_name']
}

For webform field values (e.g. type = radios):

/**
 * {@inheritdoc}
 */
public function postSave(WebformSubmissionInterface $webform_submission, $update = TRUE) {

  // Get the webform fields (which will contain properties/attributes/values/etc.).
  $elements = $webform_submission->getWebform()->getElementsDecoded();

  // Get field values.
  $fieldType = $elements['field_name']['#type'];
  $fieldTitle = $elements['field_name']['#title'];
  $fieldFirstOption = $elements['field_name']['#options'];['option_one'] // If you need the selected value that will be in ->getData(), or in ['#return_value'] key.

  // Get webform owner (not anonymous).
  $webformOwner = $webform_submission->getWebform()->getOwner();

  // Get owner values.
  $displayName = $webformOwner->getDisplayName();
  $userName = $webformOwner->getUsername();
  $userEmail = $webformOwner->getEmail();

  // And to set data values:
  $data = $webform_submission->getData();
  $data['myNewValue'] = $myNewValue;
  $webform_submission->setData($data);

  // If you need to find some other available methods, try:
  kint($webform_submission->getWebform());
}

Fields on the webform, e.g. feedback_type, your_email, first_name will be available inside the method ->getElementsDecoded().

| improve this answer | |
  • To answer the first part, I need the fields because I'm passing those values to a separate API that we have. So I need to get the email address, name, feedback type (which is a hidden field). How would I view the available variables? I tried using kint() but nothing gets printed out most likely because it's reloading/navigating away. From the original form. – user1470118 Apr 9 at 14:39
  • I've added a bit to the answer for getting owner info. Though if the email is a field on the webform, such as for anonymous users, it will just be a field you can access. Also, you can add an exit; after your kint(); statement to stop other code from running, then just resubmit your form to see the kint readout. – Prestosaurus Apr 9 at 16:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.