You are here

private function KernelEventListener::restrictTermAccessAtAutoCompletion in Permissions by Term 8

Same name and namespace in other branches
  1. 8.2 src/Listener/KernelEventListener.php \Drupal\permissions_by_term\Listener\KernelEventListener::restrictTermAccessAtAutoCompletion()

Restricts access to terms on AJAX auto completion.

1 call to KernelEventListener::restrictTermAccessAtAutoCompletion()
KernelEventListener::onKernelResponse in src/Listener/KernelEventListener.php
Restricts access on kernel response.

File

src/Listener/KernelEventListener.php, line 103

Class

KernelEventListener
Class KernelEventListener.

Namespace

Drupal\permissions_by_term\Listener

Code

private function restrictTermAccessAtAutoCompletion(FilterResponseEvent $event) {
  if ($event
    ->getRequest()->attributes
    ->get('target_type') == 'taxonomy_term' && $event
    ->getRequest()->attributes
    ->get('_route') == 'system.entity_autocomplete') {
    $json_suggested_terms = $event
      ->getResponse()
      ->getContent();
    $suggested_terms = json_decode($json_suggested_terms);
    $allowed_terms = [];
    foreach ($suggested_terms as $term) {
      $tid = $this->term
        ->getTermIdByName($term->label);
      $termLangcode = \Drupal::languageManager()
        ->getCurrentLanguage()
        ->getId();
      if ($this->term
        ->getTerm() instanceof \Drupal\taxonomy\Entity\Term) {
        $termLangcode = $this->term
          ->getTerm()
          ->language()
          ->getId();
      }
      if ($this->accessCheckService
        ->isAccessAllowedByDatabase($tid, \Drupal::currentUser()
        ->id(), $termLangcode)) {
        $allowed_terms[] = [
          'value' => $term->value,
          'label' => $term->label,
        ];
      }
    }
    $json_response = new JsonResponse($allowed_terms);
    $event
      ->setResponse($json_response);
  }
}