Magento 2 : Call Helper Without Using __construct in Own ModuleMandatory Helper class for module admin panel...

Can we use the stored gravitational potential energy of a building to produce power?

Dilemma of explaining to interviewer that he is the reason for declining second interview

Is there any other number that has similar properties as 21?

Parsing a string of key-value pairs as a dictionary

Is there any differences between "Gucken" and "Schauen"?

How can I deliver in-universe written lore to players without it being dry exposition?

Which password policy is more secure: one password of length 9 vs. two passwords each of length 8?

Can making a creature unable to attack, after it has been assigned as an attacker, remove it from combat?

how to acknowledge an embarrasing job interview, now that I work directly with the interviewer?

How can animals be objects of ethics without being subjects as well?

My cat mixes up the floors in my building. How can I help him?

It took me a lot of time to make this, pls like. (YouTube Comments #1)

How to remove lines through the legend markers in ListPlot?

Intern applicant asking for compensation equivalent to that of permanent employee

Error in a formula field

What is the most triangles you can make from a capital "H" and 3 straight lines?

Can an insurance company drop you after receiving a bill and refusing to pay?

What kind of hardware implements Fourier transform?

Why do stocks necessarily drop during a recession?

How to deal with an incendiary email that was recalled

A starship is travelling at 0.9c and collides with a small rock. Will it leave a clean hole through, or will more happen?

Lick explanation

Indirectly access environment variable

What is better: yes / no radio, or simple checkbox?



Magento 2 : Call Helper Without Using __construct in Own Module


Mandatory Helper class for module admin panel settingsHow to override core helper in magento 2?magento 2 get product sale count using custom moduleMagento 2 : How can I call helper function on override blockMagento 2 : How can I call helper function on override product list blockHow To Override Custom Module Helper in Magento 2Magento 2 Custom New Block without moduleHow to call getReviewsCollection() function from MagentoReviewBlockProductView?Call block function from helperMagento 2 Create dynamic array From different Model Collection to use in multi select in grid













3















I have created a new module and override the




MagentoCatalogBlockProductViewDescription




function using preference.
In this module we create new helper located in




Custom_Hello/Helper/Data.php




Then how to call this helper in




Custom_Hello/Block/Product/View/Description.php




If we use __construct got some error.



public function __construct(CustomHelloHelperData $helper)
{
$this->helper = $helper;
}


Anyone helpme










share|improve this question

























  • you can use object manager method into this case, As I know that is not peoper method.

    – Dhiren Vasoya
    1 hour ago











  • I'm new in magento. But using plugin we got solution then using this way error

    – Soundararajan m
    1 hour ago











  • Could you provide the error you are receiving? The way you are injecting it is the proper approach so if we can resolve the error that would be best.

    – rain2o
    1 hour ago











  • Fatal error: Uncaught Error: Call to a member function dispatch() on null in /var/www/html/magento2/lib/internal/Magento/Framework/View/Element/AbstractBlock.php:652 Stack trace: #0 /var/www/html/magento2/lib/internal/Magento/Framework/Interception/Interceptor.php(58): MagentoFrameworkViewElementAbstractBlock->toHtml() #1

    – Soundararajan m
    1 hour ago


















3















I have created a new module and override the




MagentoCatalogBlockProductViewDescription




function using preference.
In this module we create new helper located in




Custom_Hello/Helper/Data.php




Then how to call this helper in




Custom_Hello/Block/Product/View/Description.php




If we use __construct got some error.



public function __construct(CustomHelloHelperData $helper)
{
$this->helper = $helper;
}


Anyone helpme










share|improve this question

























  • you can use object manager method into this case, As I know that is not peoper method.

    – Dhiren Vasoya
    1 hour ago











  • I'm new in magento. But using plugin we got solution then using this way error

    – Soundararajan m
    1 hour ago











  • Could you provide the error you are receiving? The way you are injecting it is the proper approach so if we can resolve the error that would be best.

    – rain2o
    1 hour ago











  • Fatal error: Uncaught Error: Call to a member function dispatch() on null in /var/www/html/magento2/lib/internal/Magento/Framework/View/Element/AbstractBlock.php:652 Stack trace: #0 /var/www/html/magento2/lib/internal/Magento/Framework/Interception/Interceptor.php(58): MagentoFrameworkViewElementAbstractBlock->toHtml() #1

    – Soundararajan m
    1 hour ago
















3












3








3








I have created a new module and override the




MagentoCatalogBlockProductViewDescription




function using preference.
In this module we create new helper located in




Custom_Hello/Helper/Data.php




Then how to call this helper in




Custom_Hello/Block/Product/View/Description.php




If we use __construct got some error.



public function __construct(CustomHelloHelperData $helper)
{
$this->helper = $helper;
}


Anyone helpme










share|improve this question
















I have created a new module and override the




MagentoCatalogBlockProductViewDescription




function using preference.
In this module we create new helper located in




Custom_Hello/Helper/Data.php




Then how to call this helper in




Custom_Hello/Block/Product/View/Description.php




If we use __construct got some error.



public function __construct(CustomHelloHelperData $helper)
{
$this->helper = $helper;
}


Anyone helpme







magento2 helper






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 1 hour ago









Dhiren Vasoya

4,32951842




4,32951842










asked 1 hour ago









Soundararajan mSoundararajan m

413




413













  • you can use object manager method into this case, As I know that is not peoper method.

    – Dhiren Vasoya
    1 hour ago











  • I'm new in magento. But using plugin we got solution then using this way error

    – Soundararajan m
    1 hour ago











  • Could you provide the error you are receiving? The way you are injecting it is the proper approach so if we can resolve the error that would be best.

    – rain2o
    1 hour ago











  • Fatal error: Uncaught Error: Call to a member function dispatch() on null in /var/www/html/magento2/lib/internal/Magento/Framework/View/Element/AbstractBlock.php:652 Stack trace: #0 /var/www/html/magento2/lib/internal/Magento/Framework/Interception/Interceptor.php(58): MagentoFrameworkViewElementAbstractBlock->toHtml() #1

    – Soundararajan m
    1 hour ago





















  • you can use object manager method into this case, As I know that is not peoper method.

    – Dhiren Vasoya
    1 hour ago











  • I'm new in magento. But using plugin we got solution then using this way error

    – Soundararajan m
    1 hour ago











  • Could you provide the error you are receiving? The way you are injecting it is the proper approach so if we can resolve the error that would be best.

    – rain2o
    1 hour ago











  • Fatal error: Uncaught Error: Call to a member function dispatch() on null in /var/www/html/magento2/lib/internal/Magento/Framework/View/Element/AbstractBlock.php:652 Stack trace: #0 /var/www/html/magento2/lib/internal/Magento/Framework/Interception/Interceptor.php(58): MagentoFrameworkViewElementAbstractBlock->toHtml() #1

    – Soundararajan m
    1 hour ago



















you can use object manager method into this case, As I know that is not peoper method.

– Dhiren Vasoya
1 hour ago





you can use object manager method into this case, As I know that is not peoper method.

– Dhiren Vasoya
1 hour ago













I'm new in magento. But using plugin we got solution then using this way error

– Soundararajan m
1 hour ago





I'm new in magento. But using plugin we got solution then using this way error

– Soundararajan m
1 hour ago













Could you provide the error you are receiving? The way you are injecting it is the proper approach so if we can resolve the error that would be best.

– rain2o
1 hour ago





Could you provide the error you are receiving? The way you are injecting it is the proper approach so if we can resolve the error that would be best.

– rain2o
1 hour ago













Fatal error: Uncaught Error: Call to a member function dispatch() on null in /var/www/html/magento2/lib/internal/Magento/Framework/View/Element/AbstractBlock.php:652 Stack trace: #0 /var/www/html/magento2/lib/internal/Magento/Framework/Interception/Interceptor.php(58): MagentoFrameworkViewElementAbstractBlock->toHtml() #1

– Soundararajan m
1 hour ago







Fatal error: Uncaught Error: Call to a member function dispatch() on null in /var/www/html/magento2/lib/internal/Magento/Framework/View/Element/AbstractBlock.php:652 Stack trace: #0 /var/www/html/magento2/lib/internal/Magento/Framework/Interception/Interceptor.php(58): MagentoFrameworkViewElementAbstractBlock->toHtml() #1

– Soundararajan m
1 hour ago












4 Answers
4






active

oldest

votes


















3














You can add your custom helper in constructor, but as this method is present in the class you're extending then you'd pass the right parameters in it, not just your helper



class Description extends MagentoCatalogBlockProductViewDescription


You are extending MagentoCatalogBlockProductViewDescription so let's see constructor in this class (in 2.3 version, you can check this in your project for other version, if that's the case)



public function __construct(
MagentoFrameworkViewElementTemplateContext $context,
MagentoFrameworkRegistry $registry,
array $data = []
) {
$this->_coreRegistry = $registry;
parent::__construct($context, $data);
}


So, in your class you'd pass that $context & $registry parameters. You can do that this way



public function __construct(
CustomHelloHelperData $helper,
MagentoFrameworkViewElementTemplateContext $context,
MagentoFrameworkRegistry $registry,
array $data = []
) {
$this->helper = $helper;
parent::__construct($context, $registry, $data);
}





share|improve this answer
























  • thanks for your comments. Already got the solution and again thanks

    – Soundararajan m
    30 mins ago



















2














I got the solution. If we use following way



public function __construct(
MagentoFrameworkViewElementTemplateContext $context,
MagentoFrameworkRegistry $registry,
CustomHelloHelperData $dataHelper,
array $data = []
) {
parent::__construct($context, $registry, $data);
$this->_dataHelper = $dataHelper;
}





share|improve this answer
























  • Answered at same time :)

    – Raul Sanchez
    30 mins ago



















2















You can use directly without contructor




$this->helper('CompanyNameModuleNameHelperData')->yourfunctionname();





share|improve this answer








New contributor




Mitrajsinh vaghela is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




























    1














    In Custom_Hello/Block/Product/View/Description.php File



    protected $helper;

    public function __construct(
    MagentoCatalogBlockProductContext $context,
    CustomHelloHelperData $helper
    array $data = []
    )
    {
    $this->helper= $heper;
    parent::__construct($context, $data);
    }


    And You Can Call Your Helper Function Like this



    $this->helper->yourFunctionName() 





    share|improve this answer























      Your Answer








      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "479"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f264051%2fmagento-2-call-helper-without-using-construct-in-own-module%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      3














      You can add your custom helper in constructor, but as this method is present in the class you're extending then you'd pass the right parameters in it, not just your helper



      class Description extends MagentoCatalogBlockProductViewDescription


      You are extending MagentoCatalogBlockProductViewDescription so let's see constructor in this class (in 2.3 version, you can check this in your project for other version, if that's the case)



      public function __construct(
      MagentoFrameworkViewElementTemplateContext $context,
      MagentoFrameworkRegistry $registry,
      array $data = []
      ) {
      $this->_coreRegistry = $registry;
      parent::__construct($context, $data);
      }


      So, in your class you'd pass that $context & $registry parameters. You can do that this way



      public function __construct(
      CustomHelloHelperData $helper,
      MagentoFrameworkViewElementTemplateContext $context,
      MagentoFrameworkRegistry $registry,
      array $data = []
      ) {
      $this->helper = $helper;
      parent::__construct($context, $registry, $data);
      }





      share|improve this answer
























      • thanks for your comments. Already got the solution and again thanks

        – Soundararajan m
        30 mins ago
















      3














      You can add your custom helper in constructor, but as this method is present in the class you're extending then you'd pass the right parameters in it, not just your helper



      class Description extends MagentoCatalogBlockProductViewDescription


      You are extending MagentoCatalogBlockProductViewDescription so let's see constructor in this class (in 2.3 version, you can check this in your project for other version, if that's the case)



      public function __construct(
      MagentoFrameworkViewElementTemplateContext $context,
      MagentoFrameworkRegistry $registry,
      array $data = []
      ) {
      $this->_coreRegistry = $registry;
      parent::__construct($context, $data);
      }


      So, in your class you'd pass that $context & $registry parameters. You can do that this way



      public function __construct(
      CustomHelloHelperData $helper,
      MagentoFrameworkViewElementTemplateContext $context,
      MagentoFrameworkRegistry $registry,
      array $data = []
      ) {
      $this->helper = $helper;
      parent::__construct($context, $registry, $data);
      }





      share|improve this answer
























      • thanks for your comments. Already got the solution and again thanks

        – Soundararajan m
        30 mins ago














      3












      3








      3







      You can add your custom helper in constructor, but as this method is present in the class you're extending then you'd pass the right parameters in it, not just your helper



      class Description extends MagentoCatalogBlockProductViewDescription


      You are extending MagentoCatalogBlockProductViewDescription so let's see constructor in this class (in 2.3 version, you can check this in your project for other version, if that's the case)



      public function __construct(
      MagentoFrameworkViewElementTemplateContext $context,
      MagentoFrameworkRegistry $registry,
      array $data = []
      ) {
      $this->_coreRegistry = $registry;
      parent::__construct($context, $data);
      }


      So, in your class you'd pass that $context & $registry parameters. You can do that this way



      public function __construct(
      CustomHelloHelperData $helper,
      MagentoFrameworkViewElementTemplateContext $context,
      MagentoFrameworkRegistry $registry,
      array $data = []
      ) {
      $this->helper = $helper;
      parent::__construct($context, $registry, $data);
      }





      share|improve this answer













      You can add your custom helper in constructor, but as this method is present in the class you're extending then you'd pass the right parameters in it, not just your helper



      class Description extends MagentoCatalogBlockProductViewDescription


      You are extending MagentoCatalogBlockProductViewDescription so let's see constructor in this class (in 2.3 version, you can check this in your project for other version, if that's the case)



      public function __construct(
      MagentoFrameworkViewElementTemplateContext $context,
      MagentoFrameworkRegistry $registry,
      array $data = []
      ) {
      $this->_coreRegistry = $registry;
      parent::__construct($context, $data);
      }


      So, in your class you'd pass that $context & $registry parameters. You can do that this way



      public function __construct(
      CustomHelloHelperData $helper,
      MagentoFrameworkViewElementTemplateContext $context,
      MagentoFrameworkRegistry $registry,
      array $data = []
      ) {
      $this->helper = $helper;
      parent::__construct($context, $registry, $data);
      }






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered 33 mins ago









      Raul SanchezRaul Sanchez

      2,09131235




      2,09131235













      • thanks for your comments. Already got the solution and again thanks

        – Soundararajan m
        30 mins ago



















      • thanks for your comments. Already got the solution and again thanks

        – Soundararajan m
        30 mins ago

















      thanks for your comments. Already got the solution and again thanks

      – Soundararajan m
      30 mins ago





      thanks for your comments. Already got the solution and again thanks

      – Soundararajan m
      30 mins ago













      2














      I got the solution. If we use following way



      public function __construct(
      MagentoFrameworkViewElementTemplateContext $context,
      MagentoFrameworkRegistry $registry,
      CustomHelloHelperData $dataHelper,
      array $data = []
      ) {
      parent::__construct($context, $registry, $data);
      $this->_dataHelper = $dataHelper;
      }





      share|improve this answer
























      • Answered at same time :)

        – Raul Sanchez
        30 mins ago
















      2














      I got the solution. If we use following way



      public function __construct(
      MagentoFrameworkViewElementTemplateContext $context,
      MagentoFrameworkRegistry $registry,
      CustomHelloHelperData $dataHelper,
      array $data = []
      ) {
      parent::__construct($context, $registry, $data);
      $this->_dataHelper = $dataHelper;
      }





      share|improve this answer
























      • Answered at same time :)

        – Raul Sanchez
        30 mins ago














      2












      2








      2







      I got the solution. If we use following way



      public function __construct(
      MagentoFrameworkViewElementTemplateContext $context,
      MagentoFrameworkRegistry $registry,
      CustomHelloHelperData $dataHelper,
      array $data = []
      ) {
      parent::__construct($context, $registry, $data);
      $this->_dataHelper = $dataHelper;
      }





      share|improve this answer













      I got the solution. If we use following way



      public function __construct(
      MagentoFrameworkViewElementTemplateContext $context,
      MagentoFrameworkRegistry $registry,
      CustomHelloHelperData $dataHelper,
      array $data = []
      ) {
      parent::__construct($context, $registry, $data);
      $this->_dataHelper = $dataHelper;
      }






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered 33 mins ago









      Soundararajan mSoundararajan m

      413




      413













      • Answered at same time :)

        – Raul Sanchez
        30 mins ago



















      • Answered at same time :)

        – Raul Sanchez
        30 mins ago

















      Answered at same time :)

      – Raul Sanchez
      30 mins ago





      Answered at same time :)

      – Raul Sanchez
      30 mins ago











      2















      You can use directly without contructor




      $this->helper('CompanyNameModuleNameHelperData')->yourfunctionname();





      share|improve this answer








      New contributor




      Mitrajsinh vaghela is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.

























        2















        You can use directly without contructor




        $this->helper('CompanyNameModuleNameHelperData')->yourfunctionname();





        share|improve this answer








        New contributor




        Mitrajsinh vaghela is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.























          2












          2








          2








          You can use directly without contructor




          $this->helper('CompanyNameModuleNameHelperData')->yourfunctionname();





          share|improve this answer








          New contributor




          Mitrajsinh vaghela is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.











          You can use directly without contructor




          $this->helper('CompanyNameModuleNameHelperData')->yourfunctionname();






          share|improve this answer








          New contributor




          Mitrajsinh vaghela is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          share|improve this answer



          share|improve this answer






          New contributor




          Mitrajsinh vaghela is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          answered 16 mins ago









          Mitrajsinh vaghelaMitrajsinh vaghela

          664




          664




          New contributor




          Mitrajsinh vaghela is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.





          New contributor





          Mitrajsinh vaghela is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.






          Mitrajsinh vaghela is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.























              1














              In Custom_Hello/Block/Product/View/Description.php File



              protected $helper;

              public function __construct(
              MagentoCatalogBlockProductContext $context,
              CustomHelloHelperData $helper
              array $data = []
              )
              {
              $this->helper= $heper;
              parent::__construct($context, $data);
              }


              And You Can Call Your Helper Function Like this



              $this->helper->yourFunctionName() 





              share|improve this answer




























                1














                In Custom_Hello/Block/Product/View/Description.php File



                protected $helper;

                public function __construct(
                MagentoCatalogBlockProductContext $context,
                CustomHelloHelperData $helper
                array $data = []
                )
                {
                $this->helper= $heper;
                parent::__construct($context, $data);
                }


                And You Can Call Your Helper Function Like this



                $this->helper->yourFunctionName() 





                share|improve this answer


























                  1












                  1








                  1







                  In Custom_Hello/Block/Product/View/Description.php File



                  protected $helper;

                  public function __construct(
                  MagentoCatalogBlockProductContext $context,
                  CustomHelloHelperData $helper
                  array $data = []
                  )
                  {
                  $this->helper= $heper;
                  parent::__construct($context, $data);
                  }


                  And You Can Call Your Helper Function Like this



                  $this->helper->yourFunctionName() 





                  share|improve this answer













                  In Custom_Hello/Block/Product/View/Description.php File



                  protected $helper;

                  public function __construct(
                  MagentoCatalogBlockProductContext $context,
                  CustomHelloHelperData $helper
                  array $data = []
                  )
                  {
                  $this->helper= $heper;
                  parent::__construct($context, $data);
                  }


                  And You Can Call Your Helper Function Like this



                  $this->helper->yourFunctionName() 






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 35 mins ago









                  Ronak RathodRonak Rathod

                  1497




                  1497






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Magento Stack Exchange!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f264051%2fmagento-2-call-helper-without-using-construct-in-own-module%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Benedict Cumberbatch Contingut Inicis Debut professional Premis Filmografia bàsica Premis i...

                      Monticle de plataforma Contingut Est de Nord Amèrica Interpretacions Altres cultures Vegeu...

                      Escacs Janus Enllaços externs Menú de navegacióEscacs JanusJanusschachBrainKing.comChessV