t() calls should be avoided in classes

Created on 4 July 2023, over 1 year ago
Updated 11 July 2023, over 1 year ago

Problem/Motivation

t() calls should be avoided in classes.
As we run the command it gives us the following warning error:

FOUND 0 ERRORS AND 1 WARNING AFFECTING 1 LINE
------------------------------------------------------------------------------------------------------------------------------------------
 318 | WARNING | t() calls should be avoided in classes, use \Drupal\Core\StringTranslation\StringTranslationTrait and $this->t() instead
------------------------------------------------------------------------------------------------------------------------------------------

Steps to reproduce

1) Install the module.

2) Run this php code standard command to detect the errors reporeted by the php code sniffer.
phpcs --standard=DrupalPractice --extensions=php,module,inc,install,test,profile,theme,css,info,txt,md,yml /path/to/drupal/example_module

3) Mention the proper path of the file in which you want to detect the error.

4) once you run the command you will get the list of errors along with the file name.

Proposed resolution

t() calls should be avoided in classes, use $this->t() instead as per php code standard.

📌 Task
Status

Needs review

Version

1.0

Component

Code

Created by

🇮🇳India Prachi6824

Live updates comments and jobs are added and updated live.
  • Coding standards

    It involves compliance with, or the content of coding standards. Requires broad community agreement.

Sign in to follow issues

Comments & Activities

  • Issue created by @Prachi6824
  • Assigned to Prachi6824
  • 🇮🇳India Prachi6824

    I will create a patch to solve the issue .

  • Issue was unassigned.
  • Status changed to Needs review over 1 year ago
  • 🇮🇳India Prachi6824

    Hello,

    I have created a patch to solve the issue.

    Please review and apply it .

  • Assigned to jay jangid
  • Issue was unassigned.
  • 🇮🇳India jay jangid Jaipur

    Hey @prachi6824 i have tested this issue in drupal 9.5.0.
    Steps followed by me:-
    1. Installed the module "search_api_pinecone".
    2. Run this php code standard command to detect the errors reporeted by the php code sniffer.
    phpcs --standard=Drupal --extensions=php,module,inc,install,test,profile,theme,css,info,txt,md,yml search_api_pinecone

    After applied this command i am getting these types of errors:-

    FILE: C:\Users\Admin\Desktop\task\search_api_pinecone\search_api_pinecone.module
    --------------------------------------------------------------------------------
    FOUND 1 ERROR AFFECTING 1 LINE
    --------------------------------------------------------------------------------
     1 | ERROR | [x] End of line character is invalid; expected "\n" but found
       |       |     "\r\n"
    --------------------------------------------------------------------------------
    PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
    --------------------------------------------------------------------------------
    
    
    FILE: ...ch_api_pinecone\src\Plugin\search_api\backend\SearchApiPineconeBackend.php
    --------------------------------------------------------------------------------
    FOUND 1 ERROR AFFECTING 1 LINE
    --------------------------------------------------------------------------------
     1 | ERROR | [x] End of line character is invalid; expected "\n" but found
       |       |     "\r\n"
    --------------------------------------------------------------------------------
    PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
    --------------------------------------------------------------------------------
    
    Time: 466ms; Memory: 10MB

    then applied this command "phpcs --standard=Drupal --extensions=php,module,inc,install,test,profile,theme,css,info,txt,md,yml search_api_pinecone"
    for remove above errors.

    see screenshot for references.

    Thank you.

Production build 0.71.5 2024