You are here

function vppr_route_access in Vocabulary Permissions Per Role 8

Route access callback.

File

./vppr.module, line 28
Vocabulary Permissions Per Role.

Code

function vppr_route_access(Route $route, RouteMatchInterface $match, AccountProxyInterface $proxy) {
  $op = $route
    ->getOption('op');
  $vocabulary = $match
    ->getParameter('taxonomy_vocabulary');
  $term_id = $match
    ->getRawParameters()
    ->getDigits('taxonomy_term');
  if (!$vocabulary && is_numeric($term_id)) {

    // Case: Add/Edit/Delete term ids.
    $vocabulary_id = \Drupal::database()
      ->select('taxonomy_term_data', 't')
      ->fields('t', [
      'vid',
    ])
      ->condition('tid', $term_id)
      ->execute()
      ->fetchField();
  }
  elseif ($vocabulary && is_string($vocabulary)) {
    $vocabulary = Vocabulary::load($vocabulary);
    $vocabulary_id = $vocabulary
      ->id();
  }
  elseif (!$vocabulary) {

    // Do nothing when vocab is null.
    $vocabulary_id = NULL;
  }
  else {
    $vocabulary_id = $vocabulary
      ->id();
  }
  if (vppr_access($op, $vocabulary_id)) {
    return AccessResult::allowed();
  }
  return AccessResult::forbidden();
}