View source
<?php
namespace Drupal\pagerer\Plugin\views\pager;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Pager\PagerManagerInterface;
use Drupal\Core\Pager\PagerParametersInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\views\Plugin\views\pager\Full;
use Symfony\Component\DependencyInjection\ContainerInterface;
class Pagerer extends Full implements ContainerFactoryPluginInterface {
protected $entityTypeManager;
protected $presetsList;
protected $presetStorage;
public function __construct(array $configuration, $plugin_id, $plugin_definition, PagerManagerInterface $pager_manager, PagerParametersInterface $pager_parameters, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $pager_manager, $pager_parameters);
$this->entityTypeManager = $entity_type_manager;
$this->presetsList = $this->entityTypeManager
->getListBuilder('pagerer_preset');
$this->presetStorage = $this->entityTypeManager
->getStorage('pagerer_preset');
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('pager.manager'), $container
->get('pager.parameters'), $container
->get('entity_type.manager'));
}
public function summaryTitle() {
if ($preset = $this->presetStorage
->load($this->options['preset'])) {
$preset_label = $preset
->label();
}
else {
$preset_label = $this
->t('n/a');
}
if (!empty($this->options['offset'])) {
return $this
->formatPlural($this->options['items_per_page'], "Using preset %preset, @count item, skip @skip", "Using preset %preset, @count items, skip @skip", [
'%preset' => $preset_label,
'@count' => $this->options['items_per_page'],
'@skip' => $this->options['offset'],
]);
}
return $this
->formatPlural($this->options['items_per_page'], "Using preset %preset, @count item", "Using preset %preset, @count items", [
'%preset' => $preset_label,
'@count' => $this->options['items_per_page'],
]);
}
public function defineOptions() {
$options = parent::defineOptions();
$options['preset'] = [
'default' => 'core',
];
return $options;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
$form['preset'] = [
'#type' => 'select',
'#title' => $this
->t('Preset'),
'#description' => $this
->t("Select the Pagerer preset to use to render the pager."),
'#options' => $this->presetsList
->listOptions(),
'#default_value' => $this->options['preset'],
];
parent::buildOptionsForm($form, $form_state);
unset($form['tags'], $form['quantity']);
}
public function render($input) {
return [
'#theme' => $this
->themeFunctions(),
'#route_name' => !empty($this->view->live_preview) ? '<current>' : '<none>',
'#element' => $this->options['id'],
'#parameters' => $input,
'#config' => [
'preset' => $this->options['preset'],
],
];
}
}