Via Parameter, the Joomla module can be flexibly adapted for end users. Parameters are variables through which Joomla is set to process certain values. In other words, parameters are influencing factors set externally to the programme. They are used to tell the module externally which data should be processed and how.
For impatient people: Look at the changed programme code in the Diff View[^codeberg.org/astrid/j4examplecode/compare/t32...t33] and copy these changes into your development version.
Step by step
New files
In this section we add parameters to the module.
New files
In this part, only files have been changed. There are no new files.
Changed files
Module
modules/mod_foo/language/en-GB/mod_foo.ini
The labelling of the parameter in the backend should adapt to the active language. For this reason we use the language file.
Are you wondering about the prefix
COM_
inCOM_MODULES_FOOPARAMS_FIELDSET_LABEL
? The language string is automatically created by Joomla because a fieldset calledfooparams
is added.
modules/mod_foo/ language/en-GB/mod_foo.ini
MOD_FOO="[PROJECT_NAME]"
MOD_FOO_XML_DESCRIPTION="Foo Module"
MOD_FOO_FIELD_URL_LABEL="URL"
COM_MODULES_FOOPARAMS_FIELDSET_LABEL="Foo Parameter"
modules/mod_foo/mod_foo.xml
In the manifest we add the new parameter so that it is editable in the Joomla backend.
<folder>language</folder>
<filename>mod_foo.xml</filename>
</files>
<config>
<fields name="params">
<fieldset name="fooparams">
<field
name="domain"
type="url"
label="MOD_FOO_FIELD_URL_LABEL"
filter="url"
/>
</fieldset>
</fields>
</config>
</extension>
Use
<fieldset name="basic">
to display the parameters in the first tab that opens immediately.
In addition to the parameters that a developer inserts into his module, there are standard parameters that Joomla handles itself. For example /administrator/components/com_modules/forms/advanced.xml
.
modules/mod_foo/tmpl/default.php
In the module's own template file, we can now access the value of the parameter. In the following example, we output the value as text. Usually a parameter is used in a more complex way, for example within control structures such as if-statements or loops.
An example of the more complex use of a parameter is a digital map where parameters are used to enable controls such as
locate me
or a choice of map type.
modules/mod_foo/tmpl/default.php
\defined('_JEXEC') or die;
echo '[PROJECT_NAME]' . $test;
echo '[PROJECT_NAME]' . $test . '<br />' . $url;
modules/mod_foo/src/Dispatcher/Dispatcher.php
The Dispatcher
collects the variables that we can use later in the module layout tmpl/default.php
. Here we see add the parameter.
{
$data = parent::getLayoutData();
$data['text'] = $this->getHelperFactory()->getHelper('FooHelper')->getText();
$data['text'] = $this->getHelperFactory()->getHelper('FooHelper')->getText($data['params']);
return $data;
}
modules/mod_foo/src/Helper/FooHelper.php
namespace FooNamespace\Module\Foo\Site\Helper;
use Joomla\Registry\Registry;
\defined('_JEXEC') or die;
public function getText()
public function getText(Registry $params)
{
return ' FooHelpertest';
$url = $params->get('domain', '-');
return ' FooHelpertest: ' . $url;
}
}
Test your Joomla module
- install the module in Joomla version 4 to test it:
Copy the files in the modules
folder into the modules
folder of your Joomla 4 installation.
A new installation is not necessary. Continue using the files from the previous part.
- Check the presence of the parameter in the backend.
- make sure that the value of the parameter is taken into account in the frontend display.
Links
Joomla Dokumentation[^docs.joomla.org/J4.x:Creating_a_Simple_Module]
Webmentions