[Variants] Element default value can't be overridden using variant

Created on 10 October 2023, about 1 year ago
Updated 8 April 2024, 9 months ago

Problem/Motivation

Overriding #default_value in variant has no effect (other element properties such as #description work fine). The problem lies in that method WebformInterface::getElementsDefaultData() (which is responsible for populating \Drupal\webform\Entity\WebformSubmission::$data) is called only once before variants are applied, but the resulting property is fetched afterwards (at src/WebformSubmissionForm.php:817) and subsequently used in \Drupal\webform\WebformSubmissionForm::populateElements() where the overridden #default_value is changed back to the default value.

Steps to reproduce

  • Create a webform, add a textarea element to it, add a default value (some string)
  • Create a variant, try to override #default_value in element overrides
  • Display the created variant of the form e.g. using a URL query parameter and see the default value is not overridden

Proposed resolution

It should be possible to override default value just as other element properties.

Remaining tasks

Decide what should be done to fix this and do it.

It looks like one possible way could be altering \Drupal\webform\Plugin\WebformVariant\OverrideWebformVariant::applyVariant to set not only element properties, but also submission data, so they're correctly applied:

$webform_submission->setElementData($element_key, $element_properties['#default_value']);
$this->setWebformSubmission($webform_submission);

The problem is that $webform_submission is not available in the WebformVariant plugin and it's not available by using WebformEntityInjectionTrait.

Another approach could possibly be creating another WebformVariant plugin, used for directly overriding WebformSubmission::$data.

πŸ› Bug report
Status

Active

Version

6.2

Component

Code

Created by

πŸ‡¨πŸ‡ΏCzech Republic milos.kroulik

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

Comments & Activities

Production build 0.71.5 2024