You are here

protected function WebformSubmissionForm::checkPrepopulateDataValid in Webform 6.x

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

Determine if element prepopulate data is valid.

Parameters

string $element_key: An element key.

string|array &$value: A value.

Return value

bool TRUE if element prepopulate data is valid.

1 call to WebformSubmissionForm::checkPrepopulateDataValid()
WebformSubmissionForm::prepopulateData in src/WebformSubmissionForm.php
Prepopulate element data.

File

src/WebformSubmissionForm.php, line 2588

Class

WebformSubmissionForm
Provides a webform to collect and edit submissions.

Namespace

Drupal\webform

Code

protected function checkPrepopulateDataValid($element_key, &$value) {

  // Make sure the element exists.
  $element = $this
    ->getWebform()
    ->getElement($element_key);
  if (!$element) {
    return FALSE;
  }

  // Make sure the element is an input.
  $element_plugin = $this->elementManager
    ->getElementInstance($element);
  if (!$element_plugin
    ->isInput($element)) {
    return FALSE;
  }

  // Validate entity references.
  // @see \Drupal\Core\Entity\Element\EntityAutocomplete::validateEntityAutocomplete
  // @see \Drupal\webform\Plugin\WebformElement\WebformTermReferenceTrait
  if ($element_plugin instanceof WebformElementEntityReferenceInterface) {
    if (isset($element['#vocabulary'])) {
      $vocabulary_id = $element['#vocabulary'];
      $options = [
        'target_type' => 'taxonomy_term',
        'handler' => 'default:taxonomy_term',
        'target_bundles' => [
          $vocabulary_id => $vocabulary_id,
        ],
      ];
    }
    elseif (isset($element['#selection_settings'])) {
      $options = $element['#selection_settings'] + [
        'target_type' => $element['#target_type'],
        'handler' => $element['#selection_handler'],
      ];
    }
    else {
      return TRUE;
    }

    /** @var \Drupal\Core\Entity\EntityReferenceSelection\SelectionInterface $handler */
    $handler = $this->selectionManager
      ->getInstance($options);
    $valid_ids = $handler
      ->validateReferenceableEntities((array) $value);
    if (empty($valid_ids)) {
      return FALSE;
    }
    else {
      $value = $element_plugin
        ->hasMultipleValues($element) ? $valid_ids : reset($valid_ids);
      return TRUE;
    }
  }

  // Validate options.
  $is_options_element = isset($element['#options']) && $element_plugin instanceof OptionsBase && !$element_plugin instanceof WebformElementOtherInterface;
  if ($is_options_element) {
    $option_values = WebformOptionsHelper::validateOptionValues($element['#options'], (array) $value);
    if (empty($option_values)) {
      return FALSE;
    }
    else {
      $value = $element_plugin
        ->hasMultipleValues($element) ? $option_values : reset($option_values);
      return TRUE;
    }
  }
  return TRUE;
}