NodeEntityBundleInfo.php in Permissions by Term 8
File
src/Service/NodeEntityBundleInfo.php
View source
<?php
namespace Drupal\permissions_by_term\Service;
use Drupal\Core\Database\Connection;
use Drupal\Core\Template\TwigEnvironment;
use Drupal\user\Entity\Role;
use Drupal\user\Entity\User;
class NodeEntityBundleInfo {
private $accessStorage;
private $term;
private $twig;
private $database;
public function __construct(AccessStorage $accessStorage, TermHandler $term, TwigEnvironment $twig, Connection $database) {
$this->accessStorage = $accessStorage;
$this->term = $term;
$this->twig = $twig;
$this->database = $database;
}
public function renderNodeDetails($viewFilePath, $langcode, $nid = null) {
$roles = null;
$users = null;
if (!empty($nid)) {
$tids = $this->term
->getTidsByNid($nid);
if (!empty($tids)) {
$uids = $this->accessStorage
->getUserTermPermissionsByTids($tids, $langcode);
$rids = $this->accessStorage
->getRoleTermPermissionsByTids($tids, $langcode);
}
}
if (!empty($rids)) {
$roles = Role::loadMultiple($rids);
}
if (!empty($uids)) {
$users = User::loadMultiple($uids);
}
$template = $this->twig
->loadTemplate($viewFilePath);
return $template
->render([
'roles' => $roles,
'users' => $users,
]);
}
public function getPermissions() {
$returnArray = null;
$permittedUsers = $this->database
->select('permissions_by_term_user', 'pu')
->fields('pu', [
'uid',
'tid',
])
->execute()
->fetchAll();
$permittedRoles = $this->database
->select('permissions_by_term_role', 'pr')
->fields('pr', [
'rid',
'tid',
])
->execute()
->fetchAll();
if (!empty($permittedRoles)) {
$returnArray['roleLabels'] = [];
foreach ($permittedRoles as $permittedRole) {
$role = Role::load($permittedRole->rid);
if (!empty($role)) {
$returnArray['roleLabels'][$permittedRole->tid][] = $role
->label();
}
}
}
if (!empty($permittedUsers)) {
$returnArray['userDisplayNames'] = [];
foreach ($permittedUsers as $permittedUser) {
$user = User::load($permittedUser->uid);
if (!empty($user)) {
$returnArray['userDisplayNames'][$permittedUser->tid][] = $user
->getDisplayName();
}
}
}
return $returnArray;
}
}