You are here

public function YamlFormAutocomplete::prepare in YAML Form 8

Prepare an element to be rendered within a form.

Parameters

array $element: An element.

\Drupal\yamlform\YamlFormSubmissionInterface $yamlform_submission: A form submission.

Overrides TextField::prepare

File

src/Plugin/YamlFormElement/YamlFormAutocomplete.php, line 36

Class

YamlFormAutocomplete
Provides a 'autocomplete' element.

Namespace

Drupal\yamlform\Plugin\YamlFormElement

Code

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

  // Query form submission for existing items.
  if (!$has_items && !empty($element['#autocomplete_existing'])) {
    $has_items = \Drupal::database()
      ->select('yamlform_submission_data')
      ->fields('yamlform_submission_data', [
      'value',
    ])
      ->condition('yamlform_id', $yamlform_submission
      ->getYamlForm()
      ->id())
      ->condition('name', $element['#yamlform_key'])
      ->condition('value', '', '!=')
      ->execute()
      ->fetchField();
  }
  if ($has_items && isset($element['#yamlform_key'])) {
    $element['#autocomplete_route_name'] = 'yamlform.element.autocomplete';
    $element['#autocomplete_route_parameters'] = [
      'yamlform' => $yamlform_submission
        ->getYamlForm()
        ->id(),
      'key' => $element['#yamlform_key'],
    ];
  }
}