You are here

protected function WebformElementController::getMatchesFromExistingValues in Webform 8.5

Same name and namespace in other branches
  1. 6.x src/Controller/WebformElementController.php \Drupal\webform\Controller\WebformElementController::getMatchesFromExistingValues()

Get matches from existing submission values.

Parameters

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

string $webform_id: The webform 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 WebformElementController::getMatchesFromExistingValues()
WebformElementController::autocomplete in src/Controller/WebformElementController.php
Returns response for the element autocomplete route.

File

src/Controller/WebformElementController.php, line 126

Class

WebformElementController
Provides route responses for Webform elements.

Namespace

Drupal\webform\Controller

Code

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

  // Query webform submission for existing values.
  $query = Database::getConnection()
    ->select('webform_submission_data')
    ->fields('webform_submission_data', [
    'value',
  ])
    ->condition('webform_id', $webform_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;
}