Skip to content

Instantly share code, notes, and snippets.

@kevinquillen
Created September 6, 2016 17:05
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 kevinquillen/78d673d01034b167a5428d4d2ec200f6 to your computer and use it in GitHub Desktop.
Save kevinquillen/78d673d01034b167a5428d4d2ec200f6 to your computer and use it in GitHub Desktop.
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