Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to pass parameters to joomla module loaded from article

I had previously added Joomla modules from within Joomla articles this way : {loadmodule mod_name} but this time I need to pass parameters from it.

How can I pass parameters from within the article to a Joomla module?

like image 932
JSFan Avatar asked Jul 31 '12 16:07

JSFan


Video Answer


2 Answers

You'll need to modify or clone the Joomla plugin loadmodule because it doesn't handle parameters other than just a module name. It's not particularly difficult to do if you're proficient in PHP (assuming you don't mind getting your hands dirty with a little bit of Regex work) but I'd suggest cloning it and using it this way:

  1. Copy folder \plugins\content\loadmodule to \plugins\content\Myloadmodule (and all it's files)
  2. Within the new folder, rename loadmodule.php and loadmodule.xml to myloadmodule.php and myloadmodule.xml. These are the files you'll do all the work on.
  3. In both files, replace occurrences of loadmodule with myloadmodule, (Case sensitive)
  4. In myloadmodule.php, start at around line 36 with the Regex that strips out what is in the {loadposition xxx} being processed. You'll need to tinker with this Regex to get the parameters that you want to supply when using {myloadmoduel blah-blah-blah} in your article.
  5. Find the database entry in your table '_extensions' for loadposition and create and identical record for myloadposition. (Unless you want to write and installer)
  6. Finally, you'll need to render the modules with your new parameters - I can't begin to help there because I don't know what modules, or parameter work you'll be doing, but this renderModule documentation will be of assistance.
  7. 7.

I think I've covered it all, but this should be enough to get most of it done for you. When you're done, use {myloadposition ...} instead of {loadposition ...}.

like image 125
GDP Avatar answered Nov 15 '22 09:11

GDP


I will give more details about the previous answer, to be able to pass parameters to a module with a tag as {loadmodule mod_name,param} The solution given by GDP works fine: it's easy and quick to rewrite a content plugin (e.g. myloadmodule), following the steps 1 to 5 in the previous answer. The problem, for me, comes with the steps 6: how to put parameters into the module, and ohow to retrieve de parameters within the module.

Step 7 : how to give parameters to the "render" of a module

In the plugin created (base on the loadmodule), you have to find the lines with the following code :

echo $renderer->render($module, $params);

Before this line, you can put parameters into the $params parameter, but "render" of the module retrieves only params with the key 'params', using a json format. So, to pass parameters to the render function, you can do something like that :

$param = array('param_name' => 'value');  // param_name = value
$params = array('params' => json_encode($param)); // 'params' (String) should be decoded by the render of the module
echo $renderer->render($module, $params);

Step 8 : How to retrieve the parameter within the module

In the helper of your module, you can retrieve the parameter with $params variable :

$value = $params->get('param_name');

To understand a helper, read this tutorial : http://docs.joomla.org/J3.3:Creating_a_simple_module/Developing_a_Basic_Module

like image 41
Frédéric Filée Avatar answered Nov 15 '22 07:11

Frédéric Filée