You are here

class TermHandler in Permissions by Term 8

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

Class Term

@package Drupal\permissions_by_term\Service

Hierarchy

Expanded class hierarchy of TermHandler

1 file declares its use of TermHandler
KernelEventListener.php in src/Listener/KernelEventListener.php
1 string reference to 'TermHandler'
permissions_by_term.services.yml in ./permissions_by_term.services.yml
permissions_by_term.services.yml
1 service uses TermHandler
permissions_by_term.term_handler in ./permissions_by_term.services.yml
\Drupal\permissions_by_term\Service\TermHandler

File

src/Service/TermHandler.php, line 14

Namespace

Drupal\permissions_by_term\Service
View source
class TermHandler {

  /**
   * The database connection.
   *
   * @var Connection
   */
  private $database;

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

  /**
   * Term constructor.
   *
   * @param Connection $database
   */
  public function __construct(Connection $database) {
    $this->database = $database;
  }

  /**
   * @param int $nid
   *
   * @return array
   */
  public function getTidsByNid($nid) {
    $query = $this->database
      ->select('taxonomy_index', 'ti')
      ->fields('ti', [
      'tid',
    ])
      ->condition('ti.nid', $nid);
    return $query
      ->execute()
      ->fetchCol();
  }

  /**
   * @param array $tids
   *
   * @return array
   */
  public function getNidsByTids($tids) {
    if (!empty($tids)) {
      $query = $this->database
        ->select('taxonomy_index', 'ti')
        ->fields('ti', [
        'nid',
      ])
        ->condition('ti.tid', $tids, 'IN');
      $nids = $query
        ->execute()
        ->fetchCol();
      return array_unique($nids);
    }
    else {
      return [];
    }
  }

  /**
   * @param string $sTermName
   *
   * @return int|null
   */
  public function getTermIdByName($sTermName) {
    $sTermName = Html::decodeEntities($sTermName);
    $aTermId = \Drupal::entityQuery('taxonomy_term')
      ->condition('name', $sTermName . '%', 'LIKE')
      ->execute();
    if (!empty($aTermId)) {
      $term = \Drupal::entityTypeManager()
        ->getStorage('taxonomy_term')
        ->load(key($aTermId));
      if ($term instanceof TermEntity) {
        $this
          ->setTerm($term);
      }
      return key($aTermId);
    }
    return null;
  }

  /**
   * @param int $term_id
   *
   * @return string
   */
  public function getTermNameById($term_id) {
    $term_name = \Drupal::entityQuery('taxonomy_term')
      ->condition('id', $term_id)
      ->execute();
    return key($term_name);
  }
  public function setTerm(TermEntity $term) {
    $this->term = $term;
  }

  /**
   * @return TermEntity
   */
  public function getTerm() {
    return $this->term;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
TermHandler::$database private property The database connection.
TermHandler::$term private property
TermHandler::getNidsByTids public function
TermHandler::getTerm public function
TermHandler::getTermIdByName public function
TermHandler::getTermNameById public function
TermHandler::getTidsByNid public function
TermHandler::setTerm public function
TermHandler::__construct public function Term constructor.