To use Pagination
class without causing conflicts with current component's links, pass the fourth argument when instantiating the class. This is a prefix to be used for generating links. Use the same prefix when getting input variables.
defined('_JEXEC') or die;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Pagination\Pagination;
use Joomla\CMS\Plugin\CMSPlugin;
use Joomla\CMS\Uri\Uri;
use Joomla\Filesystem\Folder;
class PlgContentPagebreakgallery extends CMSPlugin
{
protected $app;
public function onContentPrepare($context, &$item, &$params, $itemPage = 0)
{
if (!preg_match('#\{pagebreakgallery=\"(.*?)\"\}#i', $item->text, $folder))
{
// Shortcode not found.
return true;
}
// Remove the shortcode.
$item->text = str_replace($folder[0], '', $item->text);
// Check that we are on article view.
if ($this->app->input->get('option') !== 'com_content' || $this->app->input->get('view') !== 'article')
{
return true;
}
$imagesDir = '/images/' . $folder[1] . '/';
try
{
$images = Folder::files(JPATH_ROOT . $imagesDir, '(?i:.(jpg|jpeg|png|gif))$');
}
catch(\Exception $e)
{
// Directory doesn't exist.
return true;
}
if (!$pages = count($images))
{
// No images.
return true;
}
// Prefix used for pagination links.
$prefix = 'pagebreakgallery_';
$page = $this->app->input->getInt($prefix . 'limitstart', 0);
if ($page > $pages - 1 || $page < 0)
{
// Image doesn't exist.
throw new \Exception(Text::_('JERROR_PAGE_NOT_FOUND'), 404);
}
$item->text = '<img src="' . Uri::root(true) . $imagesDir . $images[$page] . '" />';
$pagination = new Pagination($pages, $page, 1, $prefix);
$item->text .= '
<div class="paging">
<div class="pagecount">' . $pagination->getPagesCounter() . '</div>
<div class="pager">' . $pagination->getPagesLinks() . '</div>
</div>';
}
}