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 18 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 @@ -113,7 +113,9 @@
<?php echo JLayoutHelper::render('joomla.content.language', $item); ?>
</td>
<td class="hidden-phone">
<a class="js-modid-insert btn btn-small btn-block btn-success" href="#" data-modid="<?php echo $item->id; ?>" data-title="<?php echo $this->escape($item->title); ?>" data-editor="<?php echo $this->escape($editor); ?>">
<?php echo (int) $item->id; ?>
</a>
</td>
</tr>
<?php endforeach; ?>
Expand Down
44 changes: 44 additions & 0 deletions libraries/src/Helper/ModuleHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -644,4 +644,48 @@ 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)
{
$app = \JFactory::getApplication();
$groups = implode(',', \JFactory::getUser()->getAuthorisedViewLevels());
$lang = \JFactory::getLanguage()->getTag();
$clientId = (int) $app->getClientId();

Copy link
Contributor

Choose a reason for hiding this comment

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

Please remove all the code from lone 659 to 693 as it is not used in the method

Copy link
Contributor Author

Choose a reason for hiding this comment

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

true again

$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;
}
}
21 changes: 20 additions & 1 deletion media/com_modules/js/admin-modules-modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ document.addEventListener('DOMContentLoaded', function() {

/** Get the elements **/
var modulesLinks = document.querySelectorAll('.js-module-insert'), i,
positionsLinks = document.querySelectorAll('.js-position-insert');
positionsLinks = document.querySelectorAll('.js-position-insert'),
idLinks = document.querySelectorAll('.js-modid-insert');

/** Assign listener for click event (for single module insertion) **/
for (i= 0; modulesLinks.length > i; i++) {
Expand Down Expand Up @@ -46,4 +47,22 @@ document.addEventListener('DOMContentLoaded', function() {
window.parent.jModalClose();
});
}

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

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

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.

50 changes: 49 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,33 @@ protected function _loadmod($module, $title, $style = 'none')

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

/**
* Loads and renders the module
*
* @param string $id The id of the module
* @param string $style The style assigned to the module
*
* @return mixed
*
* @since __DEPLOY_VERSION__
*/
protected function _loadid($id, $style = 'none')
Copy link
Contributor

@Quy Quy Jan 15, 2018

Choose a reason for hiding this comment

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

The syntax {loadmoduleid 12345} does not allow style. Remove $style parameter or accommodate for it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

removed

{
self::$modules[$id] = '';
$document = JFactory::getDocument();
$renderer = $document->loadRenderer('module');
$modules = JModuleHelper::getModuleById($id);
$params = array('style' => $style);
ob_start();

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

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

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