4

Joomla 3

I was trying to modify Joomla's pagination output. Following this documentation, I create mytemplate/html/pagination.php file and start modifying it. According to the document, I can write 4 functions in the new pagination file, I actually just want to rewrite _list_render() and _item_active(). However, in libraries/cms/pagination/pagination.php beginning from line 327

    $chromePath = JPATH_THEMES . '/' . $this->app->getTemplate() . '/html/pagination.php';

    if (file_exists($chromePath))
    {
        include_once $chromePath;

        if (function_exists('pagination_item_active') && function_exists('pagination_item_inactive'))
        {
            $itemOverride = true;
        }

        if (function_exists('pagination_list_render'))
        {
            $listOverride = true;
        }
    }

This means I can only get CMS to use my function if only both pagination_item_active() and pagination_item_inactive() exist. But I don't have my own pagination_item_active(), so I copy the content of _item_active() in the CMS's pagination.php to my override file so it looks like this in my pagination.php

......
function pagination_item_active(JPaginationObject $item){
  $title = '';
  $class = '';

  if (!is_numeric($item->text))
  {
    JHtml::_('bootstrap.tooltip');
    $title = ' title="' . $item->text . '"';
    $class = 'hasTooltip ';
  }

  if ($this->app->isAdmin())
  {
    return '<a' . $title . ' href="#" onclick="document.adminForm.' . $this->prefix
    . 'limitstart.value=' . ($item->base > 0 ? $item->base : '0') . '; Joomla.submitform();return false;">' . $item->text . '</a>';
  }
  else
  {
    return '<a' . $title . ' href="' . $item->link . '" class="' . $class . 'pagenav">' . $item->text . '</a>';
  }

}

The problem is, when I run the PHP, I get this error message:

Using $this when not in object context in

/home/w/public_html/templates/rt_photon/html/pagination.php on line 56

What I don't understand is, my pagination.php is supposed to be included into the CMS's one, where there is an "object context", why then is it giving this error message? And how do I overcome this dillema?

0

2 Answers 2

1

The libraries/cms/pagination/pagination.php defines the JPagination class and I don't know exactly how overrides are merged in this class, anyway you could grab a pagination example from protostar default template that, as you see, doesn't use any reference to the main JPagination object and uses only the $item in parameters:

function pagination_item_active(&$item){
    $class = '';

    // Check for "Start" item
    if ($item->text == JText::_('JLIB_HTML_START')){
        $display = '<span class="icon-first"></span>';
    }

    // Check for "Prev" item
    if ($item->text == JText::_('JPREV')){
        $display = '<span class="icon-previous"></span>';
    }

    // Check for "Next" item
    if ($item->text == JText::_('JNEXT')){
        $display = '<span class="icon-next"></span>';
    }

    // Check for "End" item
    if ($item->text == JText::_('JLIB_HTML_END')){
        $display = '<span class="icon-last"></span>';
    }

    // If the display object isn't set already, just render the item with its text
    if (!isset($display)){
        $display = $item->text;
        $class   = ' class="hidden-phone"';
    }

    return '<li' . $class . '><a title="' . $item->text . '" href="' . $item->link . '" class="pagenav">' . $display . '</a></li>';
}
1

I'm no PHP expert but I'll try to give a small technical reason as to what I believe the issue is in your case.

$this->app->isAdmin() is causing the error because the only object that is being passed through the function is JPagination, which is the $item variable, not $this.

Therefore to get the JApplication object, you'll need to manually define it like so:

$app = JFactory::getApplication();

Then perform your check, like so:

if ($app->isAdmin())
{
    // Your code
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.