How to get module parameters inside helper function and how to get params inside a custom form field type?

Isaiah: 2 weeks ago

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();

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:


//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.

Landon: 2 weeks ago

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.

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.