Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example of creating a reusable object that provides the Drupal pager easily. See SearchResults.php for an example.
<?php
namespace Drupal\velirsearch\Service;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\HttpFoundation\RequestStack;
class PagerService implements ContainerAwareInterface {
use ContainerAwareTrait;
/**
* @var \Symfony\Component\HttpFoundation\Request
*/
private $requestStack;
/**
* PagerService constructor.
* @param \Symfony\Component\HttpFoundation\RequestStack $requestStack
*/
public function __construct(RequestStack $requestStack) {
$this->requestStack = $requestStack;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
$requestStack = $container->get('request_stack');
return new static($requestStack);
}
/**
* @param $totalRows
* @param $limit
* @return array
*/
public function getPager($totalRows, $limit = 10) {
pager_default_initialize($totalRows, $limit);
return ['#type' => 'pager'];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment