Created
September 6, 2016 17:05
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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