You are here

public function AutocompleteTags::handleAutocomplete in Business Rules 2.x

Same name and namespace in other branches
  1. 8 src/Controller/AutocompleteTags.php \Drupal\business_rules\Controller\AutocompleteTags::handleAutocomplete()

Handler for autocomplete request.

Parameters

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

Return value

\Symfony\Component\HttpFoundation\JsonResponse The matched values.

1 string reference to 'AutocompleteTags::handleAutocomplete'
business_rules.routing.yml in ./business_rules.routing.yml
business_rules.routing.yml

File

src/Controller/AutocompleteTags.php, line 30

Class

AutocompleteTags
Class AutocompleteTags.

Namespace

Drupal\business_rules\Controller

Code

public function handleAutocomplete(Request $request) {
  $matches = [];
  $string = $request->query
    ->get('q');

  // Get the current saved tags.
  $br_tags = BusinessRule::loadAllTags();
  $ac_tags = Action::loadAllTags();
  $co_tags = Condition::loadAllTags();
  $va_tags = Variable::loadAllTags();
  $tags = array_merge($br_tags, $ac_tags, $co_tags, $va_tags);

  // Keep track of previously processed tags so they can be skipped.
  foreach ($tags as $tag) {
    if (strpos($tag, $string) === 0) {
      $matches[] = [
        'value' => $tag,
        'label' => Html::escape($tag),
      ];
      if (count($matches) >= 10) {
        break;
      }
    }
  }
  return new JsonResponse($matches);
}