5

Part 1:

How to get module parameters inside helper function?

I am trying to get the same module parameters inside the helper file.

class ModTestHelper
{
    public function getFoo () {
        return $this;
    }
}

And then calling the getFoo() method in another file:

$result = ModTestHelper::getFoo();
print_r($result);

Tells me Non-static method ModTestHelper::getFoo() should not be called statically though it lists the result for $this.

But if I use static function:

public static function getFoo () {
    return $this;
}

Then it would obviously tell me undefined variable $this. Sorry self also don't work.

I also tried with new instance instead of ModTestHelper::getFoo() but no luck.

Part 2:

How to get module parameters inside my module's custom form field type?

I am trying to use helper method inside fields:

mod_test/models/fields/foo.php


//I have called require statement for helper 
// file before class declaration
public function getInput()
    {
        //helper method here
    }

So, I think there is another way to get the parameters of the module in the helper file.

  • 1
    Both mine and Rene's answer are explaining how to do this. Also have a read here in how to develop a module and you should be in the right track. – FFrewin Jul 1 '16 at 7:03
  • How to get module parameters inside helper class is different question than how to get module parameters inside a module's custom form field type. So continuously editing the question/answers as we progress on what you actually want to do, is creating eventually a mess as of what this Question/Answers will serve here. – FFrewin Jul 1 '16 at 8:16
  • See my updated answer, let me know if that is what you are looking for. – FFrewin Jul 1 '16 at 8:31
  • I edited your question, so it now reflects both topics – FFrewin Jul 1 '16 at 10:41
6

1. How to get Modules parameters inside Helper Function

You are going all wrong with this.

First, you can't use $this inside a static method, because they are independent of any instantiated object. Static methods can be called without instantiating an object so there is no $this reference inside.

http://php.net/manual/en/language.oop5.static.php

Just for the completeness of things, you can use self::yourClassMethod(); to call other methods of your class inside your static method.

Also, your method is inside your own Class, the one that you are building there. What you are wishing the $this would give you by using it inside your helperClass, is not and couldn't be the $module instance.

a. Using a static method:

To access the params of the module to your static function, simply use the $params as your method's parameter, and then pass the $params to your method when calling it.

In your helper Class:

public static function getFoo ($params) {
    return $params;
}

Then you call your static method:

$paramsAgain = ModTestHelper::getFoo($params);

Note :

Obviously you need to call your static Method from inside a context where the $module/ $params exist, like the mod_module.php.

b. Instantiating an Object of your HelperClass

See the @Lodder's and @Rene's answers, they both contain examples on how to use your method if it's not a static one.


2. How to access module parameters in your custom form field type.

When in your custom form field, you are inside the form of your module. You can access the form with:

$moduleFormObject = $this->form;
$moduleParams = $this->form->getValue('params');
$moduleID = $this->form->getValue('id');
$moduleTitle = $this->form->getValue('title');
$moduleParamsMyField = $this->form->getValue('MyField', 'params'); // * params is the container of where the MyField is sitting.
  • undefined variable $params. – Bhojendra Rauniyar Jul 1 '16 at 6:50
  • @BhojendraNepal From which file you pass the $params? Obviously you need to call your static Method from inside a context where the $module/ $params exist, like the mod_module.php. – FFrewin Jul 1 '16 at 6:52
  • could you please check my edited question again. – Bhojendra Rauniyar Jul 1 '16 at 7:07
  • @BhojendraNepal, I will post an answer later on this if nobody else does it, as I am on my way to leave now. – FFrewin Jul 1 '16 at 7:08
  • @BhojendraNepal, since the scope of the question has changed and the original question has been answered - maybe post a new question with your exact requirement. – FFrewin Jul 1 '16 at 7:10
6

I much prefer going for the OOP approach. This is what I use in one of my extensions. It's Ajax based so I have a separate function to get the parameters.

helper.php:

class ModSomethingHelper
{
    private $params = null;

    public function __construct($title)
    {
        $this->params = $this->getParams($title);
    }

    public function getParams($title = null)
    {
        jimport('joomla.application.module.helper');
        $module = JModuleHelper::getModule('mod_something', $title);
        $moduleParams = new JRegistry;
        $moduleParams->loadString($module->params);
        return $moduleParams;
    }

    public function test()
    {
        return $this->params->get('something');
    }
}

mod_something.php:

// Include the helper
require_once dirname(__FILE__) . '/helper.php';

// Get the title of the module to pass through the constructor
$title = $module->title;

// Initiate the helper
$helper = new ModSomethingHelper($title);

Echo result from the test() function
echo $helper->test();

Update:

To get the parameters in a custom form field, you can use the following:

fields/myfield.php:

class JFormFieldMyfield extends JFormField
{
    protected $type = 'Myfield';

    protected function getInput()
    {
        $input = JFactory::getApplication()->input;

        // Get and initiate helper
        require_once dirname(__FILE__) . './../helper.php';
        $helper = new ModSomethingHelper($input->get('id'));

        // Get params object
        $params = $helper->getParams();
    }
}
  • plus 1 for your answer. I'll try this later. – Bhojendra Rauniyar Jul 1 '16 at 10:19
  • 1
    @BhojendraNepal, you said you want to access the module's $params from inside your custom form field. Lodder's answer is about what the title of your question says and to call your moduleHelper Class from inside mod_module.php and is giving solution similar as mine and rene's answer ( - that's why I told you for posting a new question- ). Using a static method or not is a preference or a choice based on how you want to use a method, but it's not going to change the context of where you can use it, nor is going to work on your scenario. – FFrewin Jul 1 '16 at 10:34
  • @Lodder, your answer is correct reading the essential parts of the OP's question, but he eventually changed what he actually needed, as he wants to get module's params inside his custom form field. – FFrewin Jul 1 '16 at 10:37
  • @FFrewin Oh! Thanks for the info. – Bhojendra Rauniyar Jul 1 '16 at 10:37
  • Oh. In which case see my updated answer @BhojendraNepal – Lodder Jul 1 '16 at 10:49
4

Your main module file and template file will have $params variable created by Joomla! core.

If you have static methods, then pass that variable to function call:

class ModTestHelper
{
    public static function getFoo ($params) {
        return $notthis; // whatever your return
    }
}


$result = ModTestHelper::getFoo($params);

But if you want to use OOP:

class ModTestHelper
{
    // Holds params
    public $params = null;

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

    public function getFoo () {
        // Params
        $someParam = $this->params->get('paramName');

        return $notthis; // whatever your return
    }
}


$helper = new ModTestHelper($params);
$result = $helper->getFoo();
  • I tried using $params but it tells me undefined variable. – Bhojendra Rauniyar Jul 1 '16 at 6:52
  • Are you calling your helper in module main file? mod_*.php? – Rene Korss Jul 1 '16 at 6:54
  • Can you copy-paste exact error message? – Rene Korss Jul 1 '16 at 6:57
  • Actually I am calling helper function inside fields (as you suggested second method) : public function getInput(){ //here – Bhojendra Rauniyar Jul 1 '16 at 6:58
  • @BhojendraNepal: So what you get if you print_r($params) inside your mod_module.php file ? – FFrewin Jul 1 '16 at 6:58

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.