You are here

public function EntityMatcher::execute in Linkit 8.5

Executes the matcher.

Parameters

string $string: The string that contains the text to search for.

Return value

\Drupal\linkit\Suggestion\SuggestionCollection A suggestion collection.

Overrides MatcherInterface::execute

File

src/Plugin/Linkit/Matcher/EntityMatcher.php, line 322

Class

EntityMatcher
Provides default linkit matchers for all entity types.

Namespace

Drupal\linkit\Plugin\Linkit\Matcher

Code

public function execute($string) {
  $suggestions = new SuggestionCollection();
  $query = $this
    ->buildEntityQuery($string);
  $query_result = $query
    ->execute();
  $url_results = $this
    ->findEntityIdByUrl($string);
  $result = array_merge($query_result, $url_results);

  // If no results, return an empty suggestion collection.
  if (empty($result)) {
    return $suggestions;
  }
  $entities = $this->entityTypeManager
    ->getStorage($this->targetType)
    ->loadMultiple($result);
  foreach ($entities as $entity) {

    // Check the access against the defined entity access handler.

    /** @var \Drupal\Core\Access\AccessResultInterface $access */
    $access = $entity
      ->access('view', $this->currentUser, TRUE);
    if (!$access
      ->isAllowed()) {
      continue;
    }
    $entity = $this->entityRepository
      ->getTranslationFromContext($entity);
    $suggestion = $this
      ->createSuggestion($entity);
    $suggestions
      ->addSuggestion($suggestion);
  }
  return $suggestions;
}