Joomla 4.x Tutorial - Extension Development - Modules - Namespace and Helper

Modify this post

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 Viewgithub.com/astridx/boilerplate/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/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_ in COM_MODULES_FOOPARAMS_FIELDSET_LABEL? The language string is automatically created by Joomla because a fieldset called fooparams is added.

modules/modfoo/ language/en-GB/en-GB.modfoo.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.php

In the module's initial file modules/mod_foo/ mod_foo.php, we check which value the parameter is set to and store it in a variable. This way, uncomplicated access is possible later.

modules/modfoo/ modfoo.php

$test  = FooHelper::getText();

$url = $params->get('domain');

 require ModuleHelper::getLayoutPath('mod_foo', $params->get('layout', 'default'));
modules/mod_foo/ mod_foo.xml

In the manifest we add the new parameter so that it is editable in the Joomla backend.

modules/modfoo/ modfoo.xml

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

Joomla Module Parameters

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;

Test your Joomla module

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

  1. Check the presence of the parameter in the backend.

Test Joomla Module

  1. make sure that the value of the parameter is taken into account in the frontend display.

Test Joomla Module

Links

Joomla Dokumentationhttps://docs.joomla.org/j4.x:creating_a_simple_module

Modify this post

Comments