t() calls should be avoided in classes, use DI and $this->t() instead:

DrupalPractice has found some issues with your code, but could be false positives.

FILE: ...eview_temp/src/Plugin/Field/FieldWidget/AutocompleteDeluxeWidget.php
--------------------------------------------------------------------------
FOUND 0 ERRORS AND 4 WARNINGS AFFECTING 4 LINES
--------------------------------------------------------------------------
 347 | WARNING | t() calls should be avoided in classes, use dependency
     |         | injection and $this->t() instead
 348 | WARNING | t() calls should be avoided in classes, use dependency
     |         | injection and $this->t() instead
--------------------------------------------------------------------------
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

ilchovuchkov created an issue. See original summary.

vuil’s picture

FileSize
2.4 KB
vuil’s picture

Status: Active » Needs review
vuil’s picture

Any update and/or review? Thanks.

edwardchiapet’s picture

Thanks, @vuil!

In the following changes, should self::t(...) be updated to $this->t(...) instead?

@@ -279,7 +283,7 @@ class AutocompleteDeluxeWidget extends WidgetBase implements ContainerFactoryPlu
   public static function validateInteger(&$element, FormStateInterface $form_state, &$complete_form) {
     $value = $element['#value'];
     if ($value !== '' && (!is_numeric($value) || intval($value) != $value)) {
-      $form_state->setError($element, t('%name must be an integer.', ['%name' => $element['#title']]));
+      $form_state->setError($element, self::t('%name must be an integer.', ['%name' => $element['#title']]));
     }
   }
@@ -289,7 +293,7 @@ class AutocompleteDeluxeWidget extends WidgetBase implements ContainerFactoryPlu
   public static function validateIntegerPositive(&$element, FormStateInterface $form_state, &$complete_form) {
     $value = $element['#value'];
     if ($value !== '' && (!is_numeric($value) || intval($value) != $value || $value <= 0)) {
-      $form_state->setError($element, t('%name must be a positive integer.', ['%name' => $element['#title']]));
+      $form_state->setError($element, self::t('%name must be a positive integer.', ['%name' => $element['#title']]));
     }
   }
 
vuil’s picture

FileSize
2.4 KB

Updated! Thank you.

edwardchiapet’s picture

Thank you, @vuil!

The updated patch looks great!

Will merge into 8.x-1.x-dev.

  • edwardchiapet committed ec44a57 on 8.x-1.x authored by vuil
    Issue #3071352 by vuil, edwardchiapet: t() calls should be avoided in...
edwardchiapet’s picture

Status: Needs review » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

Sergei Churilo’s picture

FileSize
1.39 KB

It is not allowed to call $this when in object context.

edwardchiapet’s picture

Status: Closed (fixed) » Needs review
edwardchiapet’s picture

Thanks, @Jabb - for catching that!

The patch looks good and I will merge into 8.x-1.x-dev.

  • edwardchiapet committed a010d76 on 8.x-1.x authored by Jabb
    Issue #3071352 by Jabb, edwardchiapet: t() calls should be avoided in...
edwardchiapet’s picture

Status: Needs review » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.