l10n_server-2.x-dev/l10n_community/src/Plugin/Block/FrontStatisticsBlock.php
l10n_community/src/Plugin/Block/FrontStatisticsBlock.php
<?php declare(strict_types=1); namespace Drupal\l10n_community\Plugin\Block; use Drupal\Core\Access\AccessResult; use Drupal\Core\Access\AccessResultInterface; use Drupal\Core\Block\BlockBase; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Session\AccountInterface; use Drupal\l10n_community\L10nStatistics; use Symfony\Component\DependencyInjection\ContainerInterface; /** * Provides a front statistics block. * * @Block( * id = "l10n_community_front_statistics", * admin_label = @Translation("Quick statistics"), * category = @Translation("L10n") * ) */ final class FrontStatisticsBlock extends BlockBase implements ContainerFactoryPluginInterface { /** * Constructs a new LanguageStatisticsBlock 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 \Drupal\l10n_community\L10nStatistics $statistics * The l10n_community.statistics service. */ public function __construct( array $configuration, $plugin_id, $plugin_definition, private readonly L10nStatistics $statistics, ) { parent::__construct($configuration, $plugin_id, $plugin_definition); } /** * {@inheritdoc} */ public static function create( ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, ): self { return new FrontStatisticsBlock( $configuration, $plugin_id, $plugin_definition, $container->get('l10n_community.statistics') ); } /** * {@inheritdoc} */ public function build(): array { $items = []; $stats_numbers = $this->statistics->getLanguageStatistics(); if (isset($stats_numbers['groups'])) { $items = [ $this->formatPlural($stats_numbers['groups'], '1 translation group', '@count translation groups'), ]; } $items = array_merge($items, [ $this->formatPlural($stats_numbers['users'] ?? 0, '1 contributor', '@count contributors'), $this->formatPlural($stats_numbers['projects'] ?? 0, '1 project managed', '@count projects managed'), $this->formatPlural($stats_numbers['releases_parsed'] ?? 0, '1 release parsed', '@count releases parsed') . ' (' . $this->formatPlural($stats_numbers['releases_queue'], '1 in queue', '@count in queue') . ')', $this->formatPlural($stats_numbers['files'] ?? 0, '1 file scanned', '@count files scanned'), $this->formatPlural($stats_numbers['strings'] ?? 0, '1 string to translate', '@count strings to translate'), $this->formatPlural($stats_numbers['translations'] ?? 0, '1 translation recorded', '@count translations recorded'), $this->formatPlural($stats_numbers['suggestions'] ?? 0, '1 suggestion awaiting approval', '@count suggestions awaiting approval'), ]); $build['content'] = [ '#theme' => 'item_list', '#items' => $items, ]; return $build; } /** * {@inheritdoc} */ protected function blockAccess(AccountInterface $account): AccessResultInterface { return AccessResult::allowedIf( $account->hasPermission('access localization community') ); } }