Upgrading to Drupal 10.4+ you might have noticed a warning in their code editor stating “Undefined function ‘t’”. While Drupal’s `t()` function remains valid in procedural code, some language analysis tools — such as Intelephense — do not automatically recognise Drupal’s global functions. This article outlines the root cause of this issue and provides three practical solutions for resolving it.
Why Intelephense displays the error
Intelephense analyses PHP files but does not have native knowledge of Drupal’s global functions. Drupal’s `t()` function, used for translation, is defined globally. Because it sits outside of a typical PHP namespace or class, Intelephense may flag it as undefined—even though Drupal handles it perfectly at runtime.
Solution one: fully qualify the translation call
The simplest way to avoid Intelephense errors is to call Drupal’s translation service explicitly rather than relying on the global function.
'label' => \Drupal::translation()->translate('Enable Scroll Navigation'),
In this approach, you explicitly invoke `\Drupal::translation()` before calling `translate()`. Because this is a fully namespaced call, Intelephense recognises it as valid PHP syntax.
Solution two: use the TranslatableMarkup class
Another straightforward method is to instantiate Drupal’s `TranslatableMarkup` class to generate the translatable string:
use Drupal\Core\StringTranslation\TranslatableMarkup;
// ...
'label' => new TranslatableMarkup('Enable Scroll Navigation'),
By importing and using the class, you clarify to Intelephense that you are creating a valid PHP object, resolving the “Undefined function” warning.
Solution three: import the global t function
If you want to continue using the familiar `t()` syntax, you can explicitly import it in your module file. For example:
use function \Drupal\t;
function scroll_navigation_install() {
// ...
\Drupal\field\Entity\FieldConfig::create([
'field_name' => 'scroll_navigation_enable',
'entity_type' => 'node',
'bundle' => $type->id(),
'label' => t('Enable Scroll Navigation'),
])->save();
}
This way, Intelephense is instructed that you are using a function that exists within the Drupal global namespace.
Which solution is best?
From Drupal’s perspective, all three solutions are valid. Each approach eliminates Intelephense’s warning, maintaining functionality in Drupal 10.x. Decide which style best suits your team:
- Service calls: Ideal for those who favour explicit namespaced method calls
- TranslatableMarkup: Useful if you want to emphasise object-oriented practices and clarity in your codebase
- Global import: Retains the `t()` syntax for developers who are used to seeing it in procedural code.
Regardless of the method you choose, your `Undefined function ‘t’` error will no longer appear, and your Drupal 10.x site can continue to benefit from the platform’s streamlined translation system.