You are here

class MpacAutocomplete in Multi-path autocomplete 8

Defines a helper class to get mpac autocompletion results.

Hierarchy

Expanded class hierarchy of MpacAutocomplete

1 string reference to 'MpacAutocomplete'
mpac.services.yml in ./mpac.services.yml
mpac.services.yml
1 service uses MpacAutocomplete
mpac.autocomplete in ./mpac.services.yml
Drupal\mpac\MpacAutocomplete

File

lib/Drupal/mpac/MpacAutocomplete.php, line 17
Contains \Drupal\mpac\MpacAutocomplete.

Namespace

Drupal\mpac
View source
class MpacAutocomplete {

  /**
   * The config factory to get the anonymous user name.
   *
   * @var \Drupal\Core\Config\ConfigFactory
   */
  protected $configFactory;

  /**
   * Constructs a MpacAutocomplete object.
   *
   * @param \Drupal\Core\Config\ConfigFactory $config_factory
   *   The config factory.
   */
  public function __construct(ConfigFactory $config_factory) {
    $this->configFactory = $config_factory;
  }

  /**
   * Get matches for the autocompletion.
   *
   * @param string $type
   *   The type of data to find (i.e. "path" or "shortcut").
   * @param string $string
   *   The string to match.
   *
   * @return array
   *   An array containing the matching items.
   */
  public function getMatches($type, $string) {
    $matches = array();
    $handlers = mpac_get_selection_handlers($type);
    if ($string) {
      $limit = $this->configFactory
        ->get('mpac.autocomplete')
        ->get('items.max');

      // Load results.
      foreach ($handlers as $handler) {
        $matches = array_merge($matches, $handler
          ->getMatchingItems($string, 'CONTAINS', $limit));
      }
    }

    // Allow other modules to alter the list of matches.
    \Drupal::moduleHandler()
      ->alter('mpac_selection_matches', $matches, $type, $string);
    return array_slice($matches, 0, $limit, TRUE);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MpacAutocomplete::$configFactory protected property The config factory to get the anonymous user name.
MpacAutocomplete::getMatches public function Get matches for the autocompletion.
MpacAutocomplete::__construct public function Constructs a MpacAutocomplete object.