View source
<?php
namespace Drupal\alpha_pagination\Plugin\views\field;
use Drupal\alpha_pagination\AlphaPagination;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\Utility\Token;
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\views\ResultRow;
use Symfony\Component\DependencyInjection\ContainerInterface;
class AlphaPaginationGroup extends FieldPluginBase {
protected $alphaPagination;
protected $token;
public function __construct(array $configuration, $plugin_id, array $plugin_definition, AlphaPagination $alpha_pagination, Token $token) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->alphaPagination = $alpha_pagination;
$this->alphaPagination
->setHandler($this);
$this->token = $token;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('alpha_pagination'), $container
->get('token'));
}
protected function defineOptions() {
$options = parent::defineOptions();
$options['label'] = '';
$options['element_label_colon'] = FALSE;
$options['exclude']['default'] = TRUE;
return $options;
}
public function submitOptionsForm(&$form, FormStateInterface $form_state) {
parent::submitOptionsForm($form, $form_state);
foreach (Element::children($form) as $child) {
if ($child === 'more' || $child === 'ui_name') {
continue;
}
$form[$child]['#access'] = FALSE;
}
}
public function query() {
}
public function render(ResultRow $values) {
$areas = $this->alphaPagination
->getAreaHandlers();
if (!$areas) {
return '';
}
$this->alphaPagination
->setHandler($areas[0]);
$path = $this->alphaPagination
->getOption('paginate_link_path');
list($entityType, $field_name) = explode('__', $this->alphaPagination
->getOption('paginate_view_field'), 2);
if (!isset($this->view->field[$field_name])) {
return '';
}
$field = $this->view->field[$field_name];
if (empty($field->last_render)) {
$field
->renderText($values);
}
$value = $this->alphaPagination
->getValue(!empty($field->last_render) ? Unicode::ucfirst(substr(strip_tags($field->last_render), 0, 1)) : '');
$label = $this->alphaPagination
->getLabel($value);
if ($value && $path && $path[0] === '#') {
$name = $this->token
->replace(substr($path, 1), $this->alphaPagination
->getTokens($value));
$label = [
'#type' => 'html_tag',
'#theme' => 'html_tag__alpha_pagination__anchor',
'#tag' => 'a',
'#value' => '',
'#attributes' => [
'name' => $name,
],
'#suffix' => $label,
];
}
return $label;
}
public function validate() {
return $this->alphaPagination
->validate();
}
}