TermHandler.php in Permissions by Term 8
File
src/Service/TermHandler.php
View source
<?php
namespace Drupal\permissions_by_term\Service;
use Drupal\Core\Database\Connection;
use Drupal\taxonomy\Entity\Term;
use Drupal\Component\Utility\Html;
class TermHandler {
private $database;
private $term;
public function __construct(Connection $database) {
$this->database = $database;
}
public function getTidsByNid($nid) {
$query = $this->database
->select('taxonomy_index', 'ti')
->fields('ti', [
'tid',
])
->condition('ti.nid', $nid);
return $query
->execute()
->fetchCol();
}
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 [];
}
}
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;
}
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;
}
public function getTerm() {
return $this->term;
}
}