You are here

protected function WebformSubmissionForm::populateElements in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/WebformSubmissionForm.php \Drupal\webform\WebformSubmissionForm::populateElements()

Populate webform elements.

Parameters

array $elements: An render array representing elements.

array $values: An array of values used to populate the elements.

1 call to WebformSubmissionForm::populateElements()
WebformSubmissionForm::form in src/WebformSubmissionForm.php
Gets the actual form array to be built.

File

src/WebformSubmissionForm.php, line 2661

Class

WebformSubmissionForm
Provides a webform to collect and edit submissions.

Namespace

Drupal\webform

Code

protected function populateElements(array &$elements, array $values) {
  foreach ($elements as $key => &$element) {
    if (!WebformElementHelper::isElement($element, $key)) {
      continue;
    }

    // If value is not set, continue to populate sub-elements.
    if (!isset($values[$key])) {
      $this
        ->populateElements($element, $values);
      continue;
    }

    // Get the element's plugin.
    $element_plugin = $this->elementManager
      ->getElementInstance($element);

    // If not input, populate sub-elements and continue.
    if (!$element_plugin || !$element_plugin
      ->isInput($element)) {
      $this
        ->populateElements($element, $values);
      continue;
    }

    // If input does not support prepopulate, populate sub-elements and continue.
    if ($this
      ->getRequest()->query
      ->has($key) && !$element_plugin
      ->hasProperty('prepopulate')) {
      $this
        ->populateElements($element, $values);
      continue;
    }

    // Determine if this is a hidden element.
    // Hidden elements use #value but need to use #default_value to
    // be populated.
    $is_hidden = $element_plugin instanceof Hidden;

    // Populate default value or value.
    if ($element_plugin
      ->hasProperty('default_value') || $is_hidden) {
      $element['#default_value'] = $values[$key];
    }
    elseif ($element_plugin
      ->hasProperty('value')) {
      $element['#value'] = $values[$key];
    }

    // API values need to trigger validation.
    if ($this->operation === 'api') {
      $element['#needs_validation'] = TRUE;
    }

    // Populate sub-elements.
    $this
      ->populateElements($element, $values);
  }
}