Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Sister issue of #2608738: Add a warning for \Drupal:: use in OO code.
t() should not be used in a controller class or a form class or a plugin class. Dependency injection should be used instead.
Wrong:
class TestForm extends FormBase {
public function buildForm($form, $form_state) {
$form['something'] = t('Example text');
}
}
Correct:
class TestForm extends FormBase {
public function buildForm($form, $form_state) {
$form['something'] = $this->t('Example text');
}
}
Comments
Comment #3
klausiOther functions commonly used that we should add?
Comment #4
dawehnerNote: t() might be still the common way in static methods like
Entity::baseFieldDefinitions
Here is a list of more methods:
Comment #5
klausianother example: Term::loadMultiple() and similar calls on Node:: or User:: for example.
Comment #9
klausiCommitted some more functions with their replacements. I think we can call this fixed now and add further functions on demand.
We also have #2647462: Include warnings about deprecated functions and methods open for stuff that is deprecated in general.