Skip to main content
How to use Pagination, example plugin
Source Link
Sharky
  • 12k
  • 8
  • 23

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>';
    }
}

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>';
    }
}
added 1 character in body
Source Link
Sharky
  • 12k
  • 8
  • 23

Disabling the plugin would work - it won't be re-enabled on updates.

But what you're doing is wrong in general. There's a fine line between making layout overrides and hacking them to a point where they no longer should be layouslayouts. For this purpose you should try to make your own component or find a 3rd party component that meets your needs.

Disabling the plugin would work - it won't be re-enabled on updates.

But what you're doing is wrong in general. There's a fine line between making layout overrides and hacking them to a point where they no longer should be layous. For this purpose you should try to make your own component or find a 3rd party component that meets your needs.

Disabling the plugin would work - it won't be re-enabled on updates.

But what you're doing is wrong in general. There's a fine line between making layout overrides and hacking them to a point where they no longer should be layouts. For this purpose you should try to make your own component or find a 3rd party component that meets your needs.

Source Link
Sharky
  • 12k
  • 8
  • 23

Disabling the plugin would work - it won't be re-enabled on updates.

But what you're doing is wrong in general. There's a fine line between making layout overrides and hacking them to a point where they no longer should be layous. For this purpose you should try to make your own component or find a 3rd party component that meets your needs.