Upgraded imagemagick and manually altered pdf to image module to handle changes....
[yaffs-website] / web / modules / contrib / permissions_by_term / src / Service / Term.php
1 <?php
2
3 namespace Drupal\permissions_by_term\Service;
4
5 use Drupal\Core\Database\Connection;
6 use Drupal\taxonomy\Entity\Term as TermEntity;
7 use Drupal\Component\Utility\Html;
8
9 /**
10  * Class Term
11  *
12  * @package Drupal\permissions_by_term\Service
13  */
14 class Term {
15
16   /**
17    * The database connection.
18    *
19    * @var Connection
20    */
21   private $database;
22
23   /**
24    * @var TermEntity
25    */
26   private $term;
27
28   /**
29    * Term constructor.
30    *
31    * @param Connection $database
32    */
33   public function __construct(
34     Connection $database
35   ) {
36     $this->database = $database;
37   }
38
39   /**
40    * @param int $nid
41    *
42    * @return array
43    */
44   public function getTidsByNid($nid) {
45     $query = $this->database->select('taxonomy_index', 'ti')
46       ->fields('ti', ['tid'])
47       ->condition('ti.nid', $nid);
48
49     return $query->execute()
50       ->fetchCol();
51   }
52
53   /**
54    * @param array $tids
55    *
56    * @return array
57    */
58   public function getNidsByTids($tids) {
59     if (!empty($tids)) {
60       $query = $this->database->select('taxonomy_index', 'ti')
61           ->fields('ti', ['nid'])
62           ->condition('ti.tid', $tids, 'IN');
63
64       $nids = $query->execute()
65         ->fetchCol();
66
67       return array_unique($nids);
68     }
69     else {
70       return [];
71     }
72   }
73
74   /**
75    * @param string $sTermName
76    *
77    * @return int
78    */
79   public function getTermIdByName($sTermName) {
80     $sTermName = Html::decodeEntities($sTermName);
81     $aTermId = \Drupal::entityQuery('taxonomy_term')
82       ->condition('name', $sTermName)
83       ->execute();
84
85     $term = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->load(key($aTermId));
86     if ($term instanceof TermEntity) {
87       $this->setTerm($term);
88     }
89
90     return key($aTermId);
91   }
92
93   /**
94    * @param int $term_id
95    *
96    * @return string
97    */
98   public function getTermNameById($term_id) {
99     $term_name = \Drupal::entityQuery('taxonomy_term')
100       ->condition('id', $term_id)
101       ->execute();
102     return key($term_name);
103   }
104
105   public function setTerm(TermEntity $term) {
106     $this->term = $term;
107   }
108
109   /**
110    * @return TermEntity
111    */
112   public function getTerm() {
113     return $this->term;
114   }
115
116 }