You are here

class NodeEntityBundleInfo in Permissions by Term 8.2

Same name and namespace in other branches
  1. 8 src/Service/NodeEntityBundleInfo.php \Drupal\permissions_by_term\Service\NodeEntityBundleInfo

Class Info

@package Drupal\permissions_by_term\Service

Hierarchy

Expanded class hierarchy of NodeEntityBundleInfo

3 files declare their use of NodeEntityBundleInfo
MultilingualTest.php in tests/src/Kernel/MultilingualTest.php
NodeEntityBundleController.php in src/Controller/NodeEntityBundleController.php
NodeEntityBundleInfoTest.php in tests/src/Kernel/NodeEntityBundleInfoTest.php
1 string reference to 'NodeEntityBundleInfo'
permissions_by_term.services.yml in ./permissions_by_term.services.yml
permissions_by_term.services.yml
1 service uses NodeEntityBundleInfo
permissions_by_term.node_entity_bundle_info in ./permissions_by_term.services.yml
\Drupal\permissions_by_term\Service\NodeEntityBundleInfo

File

src/Service/NodeEntityBundleInfo.php, line 15

Namespace

Drupal\permissions_by_term\Service
View source
class NodeEntityBundleInfo {

  /**
   * @var AccessStorage
   */
  private $accessStorage;

  /**
   * @var TermHandler
   */
  private $term;

  /**
   * @var TwigEnvironment
   */
  private $twig;

  /**
   * @var Connection
   */
  private $database;

  /**
   * Info constructor.
   *
   * @param AccessStorage   $accessStorage
   * @param TermHandler            $term
   * @param TwigEnvironment $twig
   * @param Connection      $database
   */
  public function __construct(AccessStorage $accessStorage, TermHandler $term, TwigEnvironment $twig, Connection $database) {
    $this->accessStorage = $accessStorage;
    $this->term = $term;
    $this->twig = $twig;
    $this->database = $database;
  }

  /**
   * @param $viewFilePath
   * @param null $nid
   * @param string $langcode
   *
   * @return string
   * @throws \Twig_Error_Loader
   * @throws \Twig_Error_Runtime
   * @throws \Twig_Error_Syntax
   */
  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,
    ]);
  }

  /**
   * @return array
   */
  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;
  }

}

Members