Skip to content
Snippets Groups Projects
Commit 3217f4e9 authored by Daniel Cothran's avatar Daniel Cothran
Browse files

Issue #3397900 by andileco: Switch from using Twig_Extension to AbstractExtension

parent 8a48edda
No related branches found
No related tags found
1 merge request!1Update 4 files
#Term ID to Name
The tid_to_name module provides a Twig function, tn, that allows you to fetch the name of a taxonomy term based on its Term ID (TID).
##Features
Simple Twig function for fetching term names.
Integrated with Drupal's multilingual system to return translated term names based on the current language.
##Usage
Once the module is installed and enabled, you can use the tn Twig function in your templates like so:
```
{{ tn(123) }}
```
Where 123 is the TID of the taxonomy term whose name you want to fetch. If the term exists, the function will output the term's name. If the term doesn't exist or the TID is invalid, the function will return an empty string.
This is especially useful in Views when you wish to override a title when using term contextual filters.
##Requirements
- Drupal 9 or 10.
- Taxonomy module (part of Drupal core).
TID to Name
<?php
namespace Drupal\tid_to_name;
use Drupal\Component\Utility\Html;
use Drupal\taxonomy\Entity\Term;
use Twig_Extension;
use Twig_SimpleFunction;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
/**
* Class TwigExtension.
* Provides a Twig function to get term name by TID.
*
* @package Drupal\tid_to_name
*/
class TidToNameTwigExtension extends Twig_Extension {
class TidToNameTwigExtension extends AbstractExtension {
/**
* {@inheritdoc}
* The language manager.
*
* @var \Drupal\Core\Language\LanguageManagerInterface
*/
protected $languageManager;
/**
* The entity repository.
*
* @var \Drupal\Core\Entity\EntityRepositoryInterface
*/
public function getName() {
return 'tid_to_name';
protected $entityRepository;
/**
* Constructs a new TidToNameTwigExtension.
*
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager.
* @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
* The entity repository.
*/
public function __construct(LanguageManagerInterface $language_manager, EntityRepositoryInterface $entity_repository) {
$this->languageManager = $language_manager;
$this->entityRepository = $entity_repository;
}
/**
* {@inheritdoc}
*/
public function getFunctions() {
public function getFunctions(): array {
return [
new Twig_SimpleFunction('tn', [$this, 'getTermName']),
new TwigFunction('tn', [$this, 'getTermNameByTid']),
];
}
/*
* Returns name of a term given its TID.
* Might be useful in Views to override title
* when using term contextual filters.
/**
* Returns the name of a term given its TID. Example: {{ tn(123) }}.
*
* Example: tn(457)
* Outputs: [name of the term 457]
* @param int|string $tid
* The term ID.
*
* @return string
* The term name.
*/
public function getTermName($tid) {
// Normalize if $tid is markup.
$tid = '' . $tid;
$term = Term::load($tid);
public function getTermNameByTid(int|string $tid): string {
// Ensure that TID is a valid integer.
if (!is_numeric($tid) || intval($tid) <= 0) {
return '';
}
$term = Term::load(intval($tid));
if ($term) {
$language = \Drupal::languageManager()->getCurrentLanguage()->getId();
$translated_term = \Drupal::service('entity.repository')
->getTranslationFromContext($term, $language);
$language = $this->languageManager->getCurrentLanguage()->getId();
$translated_term = $this->entityRepository->getTranslationFromContext($term, $language);
return $translated_term->getName();
}
......
services:
tid_to_name:
tid_to_name.twig_extension:
class: Drupal\tid_to_name\TidToNameTwigExtension
arguments: ['@language_manager', '@entity.repository']
tags:
- { name: twig.extension }
\ No newline at end of file
- { name: twig.extension }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment