You are here

protected function YamlFormElementController::getMatchesFromExistingValues in YAML Form 8

Get matches from existing submission values.

Parameters

string $q: String to filter option's label by.

string $yamlform_id: The form id.

string $key: The element's key.

string $operator: Match operator either CONTAINS or STARTS_WITH.

int $limit: Limit number of matches.

Return value

array An array of matches.

1 call to YamlFormElementController::getMatchesFromExistingValues()
YamlFormElementController::autocomplete in src/Controller/YamlFormElementController.php
Returns response for the element autocomplete route.

File

src/Controller/YamlFormElementController.php, line 125

Class

YamlFormElementController
Provides route responses for form element.

Namespace

Drupal\yamlform\Controller

Code

protected function getMatchesFromExistingValues($q, $yamlform_id, $key, $operator = 'CONTAINS', $limit = 10) {

  // Query form submission for existing values.
  $query = Database::getConnection()
    ->select('yamlform_submission_data')
    ->fields('yamlform_submission_data', [
    'value',
  ])
    ->condition('yamlform_id', $yamlform_id)
    ->condition('name', $key)
    ->condition('value', $operator == 'START_WITH' ? "{$q}%" : "%{$q}%", 'LIKE')
    ->orderBy('value');
  if ($limit) {
    $query
      ->range(0, $limit);
  }

  // Convert query results values to matches array.
  $values = $query
    ->execute()
    ->fetchCol();
  $matches = [];
  foreach ($values as $value) {
    $matches[$value] = [
      'value' => $value,
      'label' => $value,
    ];
  }
  return $matches;
}