l10n_server-2.x-dev/l10n_community/src/Plugin/Block/PagerAlphabeticFilterBlock.php
l10n_community/src/Plugin/Block/PagerAlphabeticFilterBlock.php
<?php declare(strict_types=1); namespace Drupal\l10n_community\Plugin\Block; use Drupal\Core\Block\BlockBase; use Drupal\Core\Link; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Url; use Drupal\l10n_server\L10nProjectManager; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\RequestStack; /** * Provides a front pager alphabetic filter block. * * @Block( * id = "l10n_community_pager_alphabetic_filter", * admin_label = @Translation("Pager alphabetic filter"), * category = @Translation("L10n") * ) */ final class PagerAlphabeticFilterBlock extends BlockBase implements ContainerFactoryPluginInterface { /** * Constructs a new LanguageProgressBlock instance. * * @param array $configuration * The plugin configuration, i.e. an array with configuration values keyed * by configuration option name. The special key 'context' may be used to * initialize the defined contexts by setting it to an array of context * values keyed by context names. * @param string $plugin_id * The plugin_id for the plugin instance. * @param mixed $plugin_definition * The plugin implementation definition. * @param \Symfony\Component\HttpFoundation\RequestStack $requestStack * The request stack. * @param \Drupal\l10n_server\L10nProjectManager $l10nProjectManager * The l10n project manager service. */ public function __construct( array $configuration, $plugin_id, $plugin_definition, private readonly RequestStack $requestStack, private readonly L10nProjectManager $l10nProjectManager, ) { parent::__construct($configuration, $plugin_id, $plugin_definition); } /** * {@inheritdoc} */ public static function create( ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, ): self { return new PagerAlphabeticFilterBlock( $configuration, $plugin_id, $plugin_definition, $container->get('request_stack'), $container->get('l10n_server.project_manager'), ); } /** * {@inheritdoc} */ public function build(): array { $items = []; $initial = '0'; if ($this->requestStack->getCurrentRequest() instanceof Request) { $initial = $this->requestStack->getCurrentRequest()->query->get('initial') ?? '0'; } foreach ($this->l10nProjectManager->getProjectInitials() as $link => $data) { $class = ['pager__item']; if ($initial === (string) $link) { $class[] = 'pager-item__current'; } $items[] = [ '#wrapper_attributes' => ['class' => $class], '#markup' => Link::fromTextAndUrl( $data['title'], Url::fromRoute('l10n_community.project.explore', [], [ 'query' => [ 'initial' => $link, ], ]))->toString(), ]; } // Only display this pager if there was over 2 items. // 'All' is the first item // and then if we only have one item to filter additionally, // we only have one possible letter. // Not much value for displaying this pager. return [ '#theme' => 'item_list', '#items' => $items, '#list_type' => 'ul', ]; } }