You are here

public function AccessStorage::addTermPermissionsByRoleIds in Permissions by Term 8.2

Same name and namespace in other branches
  1. 8 src/Service/AccessStorage.php \Drupal\permissions_by_term\Service\AccessStorage::addTermPermissionsByRoleIds()

Parameters

array $aRoleIdsGrantedAccess:

int $term_id:

string $langcode:

Throws

\Exception

1 call to AccessStorage::addTermPermissionsByRoleIds()
AccessStorage::saveTermPermissions in src/Service/AccessStorage.php

File

src/Service/AccessStorage.php, line 335

Class

AccessStorage
Class AccessStorage.

Namespace

Drupal\permissions_by_term\Service

Code

public function addTermPermissionsByRoleIds($aRoleIdsGrantedAccess, $term_id, $langcode = '') {
  $langcode = $langcode === '' ? \Drupal::languageManager()
    ->getCurrentLanguage()
    ->getId() : $langcode;
  $roles = Role::loadMultiple();
  foreach ($roles as $role => $roleObj) {
    if ($roleObj
      ->hasPermission('bypass node access')) {
      $aRoleIdsGrantedAccess[] = $roleObj
        ->id();
    }
  }
  $aRoleIdsGrantedAccess = array_unique($aRoleIdsGrantedAccess);
  foreach ($aRoleIdsGrantedAccess as $sRoleIdGrantedAccess) {
    $queryResult = $this->database
      ->query("SELECT rid FROM {permissions_by_term_role} WHERE tid = :tid AND rid = :rid AND langcode = :langcode", [
      ':tid' => $term_id,
      ':rid' => $sRoleIdGrantedAccess,
      ':langcode' => $langcode,
    ])
      ->fetchField();
    if (empty($queryResult)) {
      $this->database
        ->insert('permissions_by_term_role')
        ->fields([
        'tid',
        'rid',
        'langcode',
      ], [
        $term_id,
        $sRoleIdGrantedAccess,
        $langcode,
      ])
        ->execute();
    }
  }
}