You are here

public function WebformEntityController::autocomplete in Webform 8.5

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

Returns a webform filter webform autocomplete matches.

Parameters

\Symfony\Component\HttpFoundation\Request $request: The current request.

bool $templates: If TRUE, limit autocomplete matches to webform templates.

bool $archived: If TRUE, limit autocomplete matches to archived webforms and templates.

Return value

\Symfony\Component\HttpFoundation\JsonResponse The JSON response.

2 string references to 'WebformEntityController::autocomplete'
webform.routing.yml in ./webform.routing.yml
webform.routing.yml
webform_templates.routing.yml in modules/webform_templates/webform_templates.routing.yml
modules/webform_templates/webform_templates.routing.yml

File

src/Controller/WebformEntityController.php, line 277

Class

WebformEntityController
Provides route responses for Webform entity.

Namespace

Drupal\webform\Controller

Code

public function autocomplete(Request $request, $templates = FALSE, $archived = FALSE) {
  $q = $request->query
    ->get('q');
  $webform_storage = $this
    ->entityTypeManager()
    ->getStorage('webform');
  $query = $webform_storage
    ->getQuery()
    ->range(0, 10)
    ->sort('title');

  // Query title and id.
  $or = $query
    ->orConditionGroup()
    ->condition('id', $q, 'CONTAINS')
    ->condition('title', $q, 'CONTAINS');
  $query
    ->condition($or);

  // Limit query to templates.
  if ($templates) {
    $query
      ->condition('template', TRUE);
  }
  elseif ($this
    ->moduleHandler()
    ->moduleExists('webform_templates')) {

    // Filter out templates if the webform_template.module is enabled.
    $query
      ->condition('template', FALSE);
  }

  // Limit query to archived.
  $query
    ->condition('archive', $archived);
  $entity_ids = $query
    ->execute();
  if (empty($entity_ids)) {
    return new JsonResponse([]);
  }
  $webforms = $webform_storage
    ->loadMultiple($entity_ids);
  $matches = [];
  foreach ($webforms as $webform) {
    if ($webform
      ->access('view')) {
      $value = new FormattableMarkup('@label (@id)', [
        '@label' => $webform
          ->label(),
        '@id' => $webform
          ->id(),
      ]);
      $matches[] = [
        'value' => $value,
        'label' => $value,
      ];
    }
  }
  return new JsonResponse($matches);
}