public function TermHandler::getTidsByNid in Permissions by Term 8.2
Same name and namespace in other branches
- 8 src/Service/TermHandler.php \Drupal\permissions_by_term\Service\TermHandler::getTidsByNid()
File
- src/
Service/ TermHandler.php, line 47
Class
Namespace
Drupal\permissions_by_term\ServiceCode
public function getTidsByNid(string $nid, $node = null) : ?array {
if ($node === NULL) {
$node = Node::load($nid);
}
if (!$node instanceof NodeInterface) {
return NULL;
}
$fieldNamesWithTaxonomyTerms = [];
$fieldDefinitons = $this->entityFieldManager
->getFieldDefinitions('node', $node
->getType());
foreach ($fieldDefinitons as $fieldDefiniton) {
if ($fieldDefiniton
->getType() === 'entity_reference' && is_numeric(strpos($fieldDefiniton
->getSetting('handler'), 'taxonomy_term'))) {
$fieldNamesWithTaxonomyTerms[] = $fieldDefiniton
->getName();
}
}
$tids = [];
foreach ($fieldNamesWithTaxonomyTerms as $fieldName) {
$termTargetIdsForField = $node
->get($fieldName)
->getValue();
foreach ($termTargetIdsForField as $key => $termTargetId) {
if (isset($termTargetId['target_id'])) {
$tids[] = $termTargetId['target_id'];
}
}
}
if (!empty($tids)) {
return $tids;
}
return NULL;
}