Skip to content

Instantly share code, notes, and snippets.

@fidlerryan
Last active October 14, 2015 02:06
Show Gist options
  • Save fidlerryan/093729fed9da416f8fa4 to your computer and use it in GitHub Desktop.
Save fidlerryan/093729fed9da416f8fa4 to your computer and use it in GitHub Desktop.
Creating a Joomla Plugin for Specific Modules
class PlgContentContentSpan extends JPlugin
{
/**
* Plugin that converts:
* <p>
* <img src="/images/EDPS-ABOVE-CONTENT-optimized.jpg" alt="" width="689" height="327">
* <img src="/images/JW-ABOVE-CONTENT-optimized.jpg" alt="" width="315" height="327">
* </p>
*
* To:
* <span id="contentspan" style="background-image: url(/images/EDPS-ABOVE-CONTENT-optimized.jpg), url(/images/JW-ABOVE-CONTENT-optimized.jpg);"></span>
*/
public function onContentPrepare($context, &$row, &$params, $page = 0)
{
// if the module is custom HTML
if ($context == 'mod_custom.content') {
// make sure the custom HTML is the _contentimages module
$module = JModuleHelper::getModule('mod_custom');
$modparams = new JRegistry($module->params);
if ($modparams['moduleclass_sfx'] == '_contentimages') {
// get the HTML
$html = $row->text;
// get the image sources
$dom = new DOMDocument();
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$imgs = $xpath->query('//img');
// make the image paths absolute for the background-image url
$img1 = '/' . $imgs->item(0)->getAttribute('src');
$img2 = '/' . $imgs->item(1)->getAttribute('src');
// format the HTML
$stdStyle = 'url(' . $img1 . '), url(' . $img2 . ');';
$pieStyle = 'url(' . $img1 . ') top left no-repeat, url(' . $img2 . ') top right no-repeat;';
$contentspan = '<span id="contentspan" style="background-image: ' . $stdStyle . ' -pie-background: ' . $pieStyle . '"></span>';
// return the new HTML
$row->text = $contentspan;
return true;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment