Skip to content

Commit

Permalink
[plugin][content] - loadmodule by id (#19362)
Browse files Browse the repository at this point in the history
  • Loading branch information
alikon authored and Michael Babker committed May 12, 2018
1 parent f3c2317 commit 74924d7
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
<span class="<?php echo $iconStates[$this->escape($item->published)]; ?>" aria-hidden="true"></span>
</td>
<td class="has-context">
<a class="js-module-insert btn btn-small btn-block btn-success" href="#" data-module="<?php echo $this->escape($item->module); ?>" data-title="<?php echo $this->escape($item->title); ?>" data-editor="<?php echo $this->escape($editor); ?>">
<a class="js-module-insert btn btn-small btn-block btn-success" href="#" data-module="<?php echo $item->id; ?>" data-editor="<?php echo $this->escape($editor); ?>">
<?php echo $this->escape($item->title); ?>
</a>
</td>
Expand Down
39 changes: 39 additions & 0 deletions libraries/src/Helper/ModuleHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -644,4 +644,43 @@ public static function isAdminMultilang()

return $enabled;
}

/**
* Get module by id
*
* @param string $id The id of the module
*
* @return \stdClass The Module object
*
* @since __DEPLOY_VERSION__
*/
public static function &getModuleById($id)
{
$modules =& static::load();

$total = count($modules);

for ($i = 0; $i < $total; $i++)
{
// Match the id of the module
if ($modules[$i]->id === $id)
{
// Found it
return $modules[$i];
}
}

// If we didn't find it, create a dummy object
$result = new \stdClass;
$result->id = 0;
$result->title = '';
$result->module = '';
$result->position = '';
$result->content = '';
$result->showtitle = 0;
$result->control = '';
$result->params = '';

return $result;
}
}
10 changes: 5 additions & 5 deletions media/com_modules/js/admin-modules-modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,18 @@ document.addEventListener('DOMContentLoaded', function() {
var modulesLinks = document.querySelectorAll('.js-module-insert'), i,
positionsLinks = document.querySelectorAll('.js-position-insert');

/** Assign listener for click event (for single module insertion) **/
/** Assign listener for click event (for single module id insertion) **/
for (i= 0; modulesLinks.length > i; i++) {
modulesLinks[i].addEventListener('click', function(event) {
event.preventDefault();
var type = event.target.getAttribute('data-module'),
name = event.target.getAttribute('data-title'),
var modid = event.target.getAttribute('data-module'),
editor = event.target.getAttribute('data-editor');

/** Use the API, if editor supports it **/
if (window.parent.Joomla && window.parent.Joomla.editors && window.parent.Joomla.editors.instances && window.parent.Joomla.editors.instances.hasOwnProperty(editor)) {
window.parent.Joomla.editors.instances[editor].replaceSelection("{loadmodule " + type + "," + name + "}")
window.parent.Joomla.editors.instances[editor].replaceSelection("{loadmoduleid " + modid + "}")
} else {
window.parent.jInsertEditorText("{loadmodule " + type + "," + name + "}", editor);
window.parent.jInsertEditorText("{loadmoduleid " + modid + "}", editor);
}

window.parent.jModalClose();
Expand All @@ -46,4 +45,5 @@ document.addEventListener('DOMContentLoaded', function() {
window.parent.jModalClose();
});
}

});
2 changes: 1 addition & 1 deletion media/com_modules/js/admin-modules-modal.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 48 additions & 0 deletions plugins/content/loadmodule/loadmodule.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ public function onContentPrepare($context, &$article, &$params, $page = 0)
$regexmod = '/{loadmodule\s(.*?)}/i';
$stylemod = $this->params->def('style', 'none');

// Expression to search for(id)
$regexmodid = '/{loadmoduleid\s([1-9][0-9]*)}/i';

// Find all instances of plugin and put in $matches for loadposition
// $matches[0] is full pattern match, $matches[1] is the position
preg_match_all($regex, $article->text, $matches, PREG_SET_ORDER);
Expand Down Expand Up @@ -117,6 +120,23 @@ public function onContentPrepare($context, &$article, &$params, $page = 0)
$stylemod = $this->params->def('style', 'none');
}
}

// Find all instances of plugin and put in $matchesmodid for loadmoduleid
preg_match_all($regexmodid, $article->text, $matchesmodid, PREG_SET_ORDER);

// If no matches, skip this
if ($matchesmodid)
{
foreach ($matchesmodid as $match)
{
$id = trim($match[1]);
$output = $this->_loadid($id);

// We should replace only first occurrence in order to allow positions with the same name to regenerate their content:
$article->text = preg_replace("|$match[0]|", addcslashes($output, '\\$'), $article->text, 1);
$style = $this->params->def('style', 'none');
}
}
}

/**
Expand Down Expand Up @@ -187,4 +207,32 @@ protected function _loadmod($module, $title, $style = 'none')

return self::$mods[$module];
}

/**
* Loads and renders the module
*
* @param string $id The id of the module
*
* @return mixed
*
* @since __DEPLOY_VERSION__
*/
protected function _loadid($id)
{
self::$modules[$id] = '';
$document = JFactory::getDocument();
$renderer = $document->loadRenderer('module');
$modules = JModuleHelper::getModuleById($id);
$params = array('style' => 'none');
ob_start();

if ($modules->id > 0)
{
echo $renderer->render($modules, $params);
}

self::$modules[$id] = ob_get_clean();

return self::$modules[$id];
}
}

0 comments on commit 74924d7

Please sign in to comment.