class TermHandler in Permissions by Term 8
Same name and namespace in other branches
- 8.2 src/Service/TermHandler.php \Drupal\permissions_by_term\Service\TermHandler
Class Term
@package Drupal\permissions_by_term\Service
Hierarchy
- class \Drupal\permissions_by_term\Service\TermHandler
Expanded class hierarchy of TermHandler
1 file declares its use of TermHandler
- KernelEventListener.php in src/
Listener/ KernelEventListener.php
1 string reference to 'TermHandler'
1 service uses TermHandler
File
- src/
Service/ TermHandler.php, line 14
Namespace
Drupal\permissions_by_term\ServiceView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
TermHandler:: |
private | property | The database connection. | |
TermHandler:: |
private | property | ||
TermHandler:: |
public | function | ||
TermHandler:: |
public | function | ||
TermHandler:: |
public | function | ||
TermHandler:: |
public | function | ||
TermHandler:: |
public | function | ||
TermHandler:: |
public | function | ||
TermHandler:: |
public | function | Term constructor. |