简体   繁体   中英

Load frontend module in Joomla Backend

I have developed several modules for the frontend of my Joomla2.5 site, but they could also be handy in the backend. Is there any way to load front-end modules from the backend (by backend I mean the administrator interface)?

I have entered the following code into a form view.

$module = JModuleHelper::getModule('mod_name_of_module');
$moduleHtml = JModuleHelper::renderModule($module);
echo $moduleHtml;

But it does not give anything. If I use print_r($module) I get

stdClass Object ( [id] => 0 [title] => [module] => mod_name_of_module [position] => [content] => [showtitle] => 0 [control] => [params] => [user] => 0 [style] => none )

Which basically means that it doesn't find the module since the module I'm trying to load in this case has an ID of 136 and not 0.

Has anyone managed this? If so: HOW???

Thanks in advance and merry xmas :)

In your *.xml config file you could just simply change:

<extension type="module" version="3.1.0" client="site" method="upgrade">

to this:

<extension type="module" version="3.1.0" client="administrator" position="menu" method="upgrade">

and install/discover the module as you would normally as for a frontend extension. Then you can change the position etc and make needed changes to display your view as you would like them to render.

The problem is that it is looking for the modules associated with the application it is in, specifically the code is looking at the admin modules folder and you want it to be looking at the site modules folder. These are two stand alone applications.

https://github.com/joomla/joomla-cms/blob/master/libraries/cms/module/helper.php#L347

The easiest thing is obviously to do what the core has done and essentially provide the same modules in each of the applications. Modules are generally so small for the most part, that is barely going to be more code than solving this problem which requires a re-think of JModuleHelper.

As what Elin said, the JModuleHelper can only load the module in the backend as you invoked it in the backend. Following Elin's link, you will find the source code JModuleHelper and the actual load() function that reads the module's info from the DB. (Warning: the line might change in the future.) Here is my "hack" for my app (tested in Joomla!3.1.5):

function getModule($moduleName, $instanceTitle = null){
  $db = JFactory::getDbo();

  $query = $db->getQuery(true)
    ->select('m.id, m.title, m.module, m.position, m.content, m.showtitle, m.params')
    ->from('#__modules AS m')
    ->where(Array(
      'm.published = 1'
      , 'm.module = ' . $db->quote($moduleName)
    ));
  if ($instanceTitle){
    $query->where('m.title = ' . $db->quote($instanceTitle));
  }

  $db->setQuery($query);
  try
  {
    $modules = $db->loadObject();  // You might want to use loadObjectList() instead
  }
  catch (RuntimeException $e)
  {
    JLog::add(JText::sprintf('JLIB_APPLICATION_ERROR_MODULE_LOAD', $e->getMessage()), JLog::WARNING, 'jerror');

    $clean = array();
    return $clean;
  }
  return $modules;
}

Use case:

$module = getModule('mod_your_module', 'The name of the module instance');
$params = new JRegistry;
$params->loadString($module->params);
require_once JPATH_SITE . '/modules/mod_your_module/helper.php';  // I have a helper class to format the params before render. So I reuse this helper here.
$params = ModYourModuleHelper::getParams($params);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM