Skip to content

Instantly share code, notes, and snippets.

@hans2103
Last active November 9, 2020 18:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hans2103/a559db83e300b120d392974e6cf64fd8 to your computer and use it in GitHub Desktop.
Save hans2103/a559db83e300b120d392974e6cf64fd8 to your computer and use it in GitHub Desktop.
Clean Joomla Social Share block
<?php
// somewhere in on of your overrides
use Joomla\CMS\Layout\LayoutHelper;
// file is stored in your template /html/layouts/template/block/share.php
?><p>Share this item</p><?php
echo LayoutHelper::render('template.block.share', array('title' => 'damn good title'));
<?php
// remove this line: /templates/perfectemplate/layout/render.php
/**
* @package PerfectTemplate
* @copyright 2019 Perfect Web Team / perfectwebteam.nl
* @license GNU General Public License version 3 or later
*/
use Joomla\CMS\Factory;
defined('_JEXEC') or die;
class PWTLayout
{
/**
* Function to get PWT Layout file
*
* @param $type
* @param string $data
*
* @return string
*
* @since version
* @throws Exception
*/
public static function render($type, $data = '')
{
$template = Factory::getApplication()->getTemplate();
$jlayout = new JLayoutFile($type, JPATH_THEMES . '/' . $template . '/html/layouts/template');
return $jlayout->render($data);
}
/**
* function to get svg icon
*
* @param $type
*
* @return string
*
* @since 1.0.0
* @throws Exception
*/
public static function icon($type)
{
$template = Factory::getApplication()->getTemplate();
return file_get_contents(JPATH_THEMES . '/' . $template . '/icons/' . $type . '.svg');
}
}
<?php
// remove this line: /templates/perfectemplate/layout/template/block/share.php
/*
* @package Perfecttemplate
* @copyright 2019 Perfect Web Team / perfectwebteam.nl
* @license GNU General Public License version 3 or later
*/
use Joomla\CMS\Factory;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Uri\Uri;
defined('_JEXEC') or die;
$this->template = Factory::getApplication()->getTemplate();
require_once JPATH_THEMES . '/' . $this->template . '/helper.php';
$menu = Factory::getApplication()->getMenu();
$active = $menu->getActive();
$title = Factory::getConfig()->get('sitename');
$twitter = 'Mindfulness_nl';
$position = isset($displayData['position']) ? ' block-share--' . $displayData['position'] : '';
if ($active)
{
$title = $active->title;
if ($active->params->get('page_heading') && $active->params->get('show_page_heading'))
{
$title = $active->params->get('page_heading');
}
}
if (!empty($displayData['title']))
{
$title = rawurlencode($displayData['title']);
}
$url = Uri::current();
$array = array(
array(
'show' => true,
'name' => 'Facebook',
'url' => 'http://www.facebook.com/sharer.php?u=' . $url,
'icon' => 'facebook-f',
'data-service' => 'facebook'
),
array(
'show' => true,
'name' => 'Twitter',
'url' => 'https://twitter.com/intent/tweet?url=' . $url . '&via=' . $twitter . '&text=' . $title,
'icon' => 'twitter',
'data-service' => 'twitter'
),
array(
'show' => true,
'name' => 'E-mail',
'url' => 'mailto:?subject=%22' . $title . '%22%20op%20' . str_replace(' ', '%20', $title) . '&body=Lees%20het%20artikel%20%22' . $title . '%22%20op%20de%20' . str_replace(' ', '%20', $title) . '%20website%3A%20' . $url,
'icon' => 'mail',
'data-service' => 'mail'
),
array(
'show' => true,
'name' => 'Whatsapp',
'url' => 'whatsapp://send?text=' . $title . '%20' . urlencode($url),
'icon' => 'whatsapp',
'data-service' => 'whatsapp',
'data-action' => 'share/whatsapp/share'
),
array(
'show' => true,
'name' => 'LinkedIn',
'url' => 'https://www.linkedin.com/shareArticle?mini=true&url=' . $url . '&title=' . $title . '&',
'icon' => 'linkedin-in',
'data-service' => 'linkedin'
)
);
?>
<div class="block-share<?php echo $position; ?>">
<?php
//echo Text::_('JSHARE');
foreach ($array as $key => $value)
{
if ($value['show'] == true)
{
$url = $value['url'];
$text = PWTLayout::icon($value['icon']) . '<span>' . $value['name'] . '</span>';
$data = array(
'class' => 'js-social-share-' . $displayData->id . ' block-share__item social-share',
'data-service' => $value['data-service']
);
echo HTMLHelper::_('link', $url, $text, $data);
}
}
?>
</div>
<script>
// Vanilla JavaScript
var jsSocialShares = document.querySelectorAll(".js-social-share<?php echo '-' . $displayData->id; ?>");
if (jsSocialShares) {
[].forEach.call(jsSocialShares, function (anchor) {
anchor.addEventListener("click", function (e) {
e.preventDefault();
windowPopup(this.href, 500, 300);
});
});
}
function windowPopup(url, width, height) {
// Calculate the position of the popup so
// it’s centered on the screen.
var left = (screen.width / 2) - (width / 2),
top = (screen.height / 2) - (height / 2);
window.open(
url,
"",
"menubar=no,toolbar=no,resizable=yes,scrollbars=yes,width=" + width + ",height=" + height + ",top=" + top + ",left=" + left
);
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment