You are here

public function WebformAutocomplete::prepare in Webform 8.5

Same name and namespace in other branches
  1. 6.x src/Plugin/WebformElement/WebformAutocomplete.php \Drupal\webform\Plugin\WebformElement\WebformAutocomplete::prepare()

Prepare an element to be rendered within a webform.

Parameters

array $element: An element.

\Drupal\webform\WebformSubmissionInterface $webform_submission: A webform submission. Webform submission is optional since it is not used by composite sub elements.

Overrides TextField::prepare

See also

\Drupal\webform\Element\WebformCompositeBase::processWebformComposite

File

src/Plugin/WebformElement/WebformAutocomplete.php, line 44

Class

WebformAutocomplete
Provides a 'autocomplete' element.

Namespace

Drupal\webform\Plugin\WebformElement

Code

public function prepare(array &$element, WebformSubmissionInterface $webform_submission = NULL) {
  parent::prepare($element, $webform_submission);
  $has_items = !empty($element['#autocomplete_items']);

  // Query webform submission for existing items.
  if (!$has_items && !empty($element['#autocomplete_existing'])) {
    $has_items = \Drupal::database()
      ->select('webform_submission_data')
      ->fields('webform_submission_data', [
      'value',
    ])
      ->condition('webform_id', $webform_submission
      ->getWebform()
      ->id())
      ->condition('name', $element['#webform_key'])
      ->condition('value', '', '!=')
      ->execute()
      ->fetchField();
  }
  if ($has_items && isset($element['#webform_key'])) {
    $element['#autocomplete_route_name'] = 'webform.element.autocomplete';
    $element['#autocomplete_route_parameters'] = [
      'webform' => $webform_submission
        ->getWebform()
        ->id(),
      'key' => $element['#webform_key'],
    ];
  }
}