I want to create a module and add it to an article

I can already create the module manually and can add it to the article using php.

I can't figure out how to add a module using php. So if I have already installed mod_test how do i create an instance of it to use in the article without going manually to the admin->module manager and adding it?

  • so I'm fine with the post flight coding. I just don't know how to set up a module using php when the module is already installed – user1616338 May 12 '19 at 21:33
  • Are you trying to assign it to a page or actually embed it within an article? – Lodder May 13 '19 at 8:01

A bit confusing your questions as is, especially when you say you can add it to the article using php, but then that you can't figure out how to add it...

If I am getting you right, you want to place some php on the article's template file that will be generating a new custom instance of an installed module and will be displayed without having this module in the database.

Below is an example of doing this. In my example I am using the mod_custom (custom html module).

You can place this in the article's template, but make sure you have create a template override for your template.

$doc = JFactory::getDocument();
$module = JModuleHelper::getModule('mod_custom', 'Custom PHP Generated Instance'); // Use a such module title, so you won't load an existing module from the database.
$module->style = "System-html5";
$module->title = "PHP Generated Module";
$module->showtitle = "1";
$module->content = "This is the content of the PHP generated instance of mod_custom";

// Module Params
$moduleParams = new JRegistry($module->params);
$moduleParams->set('style', "System-html5");

// Debug the module
// echo '<pre>';
// var_dump($module);
// echo '<hr/>';
// var_dump($moduleParams);
// echo '</pre>';

//Render the module
$renderer = $doc->loadRenderer('module');
$moduleContent = $renderer->render($module, array('params'=> $moduleParams));    
echo $moduleContent;

Play and experiment with the above code and adjust it to your specific needs and according to the params of your module.

  • This is sort of what I was looking for. In an article you can add a module with {loadposition moduleid} What i want to know is how i create that module id. I want to create the module using php instead of doing it manually through extensions->modules->NEW – user1616338 May 13 '19 at 20:31
  • @user1616338 - do you want to create a new module from inside an article? It actually means for me that you would like to add some kind of a content in an article by creating a module inside an article. I also do not see the useful concept behind that at the moment. Maybe you rather need a content plugin instead of a module. – Zollie May 14 '19 at 5:10
  • I#m being confusing - how do u create a module via php ignore the article bit – user1616338 May 14 '19 at 6:32
  • @user1616338 do you want to programmatically insert a module instance in the database (so it appears in module manager) or do you want to display a module on-the-fly without having to insert it in the database? – Sharky May 14 '19 at 6:36
  • add a module so it show in the module manager - – user1616338 May 14 '19 at 14:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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