Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[plugin][content] - loadmodule by id #19362

Merged
merged 27 commits into from
Mar 17, 2018
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,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();

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove blank line.

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

49 changes: 48 additions & 1 deletion plugins/content/loadmodule/loadmodule.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ public function onContentPrepare($context, &$article, &$params, $page = 0)

// Expression to search for(modules)
$regexmod = '/{loadmodule\s(.*?)}/i';
$stylemod = $this->params->def('style', 'none');

// Expression to search for(id)
$regexmodid = '/{loadmoduleid\s([1-9][0-9]*)}/i';
$stylemod = $this->params->def('style', 'none');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move back to its original line so it is not assumed to be related to modid.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved back


// Find all instances of plugin and put in $matches for loadposition
// $matches[0] is full pattern match, $matches[1] is the position
Expand Down Expand Up @@ -117,6 +120,22 @@ 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 +206,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];
}
}