You are here

class AutocompleteHelper in Search API Autocomplete 8

Provides helper methods for creating autocomplete suggestions.

Hierarchy

Expanded class hierarchy of AutocompleteHelper

2 files declare their use of AutocompleteHelper
AccessTest.php in tests/src/Unit/AccessTest.php
AutocompleteHelperTest.php in tests/src/Unit/AutocompleteHelperTest.php
1 string reference to 'AutocompleteHelper'
search_api_autocomplete.services.yml in ./search_api_autocomplete.services.yml
search_api_autocomplete.services.yml
1 service uses AutocompleteHelper
search_api_autocomplete.helper in ./search_api_autocomplete.services.yml
Drupal\search_api_autocomplete\Utility\AutocompleteHelper

File

src/Utility/AutocompleteHelper.php, line 14

Namespace

Drupal\search_api_autocomplete\Utility
View source
class AutocompleteHelper implements AutocompleteHelperInterface {

  /**
   * The element info manager.
   *
   * @var \Drupal\Core\Render\ElementInfoManagerInterface
   */
  protected $elementInfo;

  /**
   * Constructs a new class instance.
   *
   * @param \Drupal\Core\Render\ElementInfoManagerInterface $element_info
   *   The element info manager.
   */
  public function __construct(ElementInfoManagerInterface $element_info) {
    $this->elementInfo = $element_info;
  }

  /**
   * {@inheritdoc}
   */
  public function splitKeys($keys) {
    $keys = ltrim($keys);

    // If there is whitespace or a quote on the right, all words have been
    // completed.
    if (rtrim($keys, " \"") != $keys) {
      return [
        rtrim($keys, ' '),
        '',
      ];
    }
    if (preg_match('/^(.*?)\\s*"?([\\S]*)$/', $keys, $m)) {
      return [
        $m[1],
        $m[2],
      ];
    }
    return [
      '',
      $keys,
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function alterElement(array &$element, SearchInterface $search, array $data = []) {
    $element['#type'] = 'search_api_autocomplete';
    $element['#search_id'] = $search
      ->id();
    $element['#additional_data'] = $data;

    // In case another module (for instance, Better Exposed Filters) adds a
    // "#process" key for our element type, make sure it is present on this
    // element now, too.
    $info = $this->elementInfo
      ->getInfo('search_api_autocomplete');
    if (!empty($info['#process'])) {
      $old_process = $element['#process'] ?? [];
      $element['#process'] = array_merge($old_process, $info['#process']);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function access(SearchInterface $search_api_autocomplete_search, AccountInterface $account) {
    $search = $search_api_autocomplete_search;
    $permission = 'use search_api_autocomplete for ' . $search
      ->id();
    $access = AccessResult::allowedIf($search
      ->status())
      ->andIf(AccessResult::allowedIf($search
      ->hasValidIndex() && $search
      ->getIndex()
      ->status()))
      ->andIf(AccessResult::allowedIfHasPermissions($account, [
      $permission,
      'administer search_api_autocomplete',
    ], 'OR'))
      ->addCacheableDependency($search);
    if ($access instanceof AccessResultReasonInterface) {
      $access
        ->setReason("The \"{$permission}\" permission is required and autocomplete for this search must be enabled.");
    }
    return $access;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AutocompleteHelper::$elementInfo protected property The element info manager.
AutocompleteHelper::access public function Checks access to the autocompletion route. Overrides AutocompleteHelperInterface::access
AutocompleteHelper::alterElement public function Alters a textfield form element to use autocompletion. Overrides AutocompleteHelperInterface::alterElement
AutocompleteHelper::splitKeys public function Splits a string with search keywords into two parts. Overrides AutocompleteHelperInterface::splitKeys
AutocompleteHelper::__construct public function Constructs a new class instance.