Skip to main content

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:

  1. Service calls: Ideal for those who favour explicit namespaced method calls
  2. TranslatableMarkup: Useful if you want to emphasise object-oriented practices and clarity in your codebase
  3. 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.

Related articles

Andrew Fletcher04 Apr 2025
Managing .gitignore changes
When working with Git, the .gitignore file plays a critical role in controlling which files and folders are tracked by version control. Yet, many developers are unsure when changes to .gitignore take effect and how to manage files that are already being tracked. This uncertainty can lead to...
Andrew Fletcher17 Mar 2025
Upgrading to PHP 8.4 challenges with Drupal contrib modules
The upgrade from PHP 8.3.14 to PHP 8.4.4 presents challenges for Drupal 10.4 websites, particularly when dealing with contributed modules. While Drupal core operates seamlessly, various contrib modules have not yet been updated to accommodate changes introduced in PHP 8.4.x. This has resulted in...
Andrew Fletcher20 Feb 2025
Handling duplicate records in Drupal
Duplicate records in Drupal can cause significant issues, particularly when they lead to integrity constraint violations in the database. These errors often occur due to duplicate UUIDs in the `node` table, which can result from programmatic imports, migrations, or unintended database...