l10n_server-2.x-dev/l10n_community/src/Plugin/Block/TopContributorsBlock.php
l10n_community/src/Plugin/Block/TopContributorsBlock.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\Cache\Cache; use Drupal\Core\Language\LanguageManagerInterface; use Drupal\Core\Link; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\Session\AccountInterface; use Drupal\group\Entity\Group; use Drupal\l10n_community\L10nStatistics; use Symfony\Component\DependencyInjection\ContainerInterface; /** * Provides a top contributors block. * * @Block( * id = "l10n_community_top_contributors", * admin_label = @Translation("Top contributors"), * category = @Translation("L10n") * ) */ class TopContributorsBlock extends BlockBase implements ContainerFactoryPluginInterface { /** * The l10n_community.statistics service. * * @var \Drupal\l10n_community\L10nStatistics */ protected L10nStatistics $statistics; /** * The route match. * * @var \Drupal\Core\Routing\RouteMatchInterface */ private $routeMatch; /** * The language manager. * * @var \Drupal\Core\Language\LanguageManagerInterface */ private LanguageManagerInterface $languageManager; /** * Constructs a new ProjectStatisticsBlock 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\Core\Routing\RouteMatchInterface $route_match * The route match. * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager * The language manager. * @param \Drupal\l10n_community\L10nStatistics $statistics * The l10n_community.statistics service. */ public function __construct( array $configuration, $plugin_id, $plugin_definition, RouteMatchInterface $route_match, LanguageManagerInterface $language_manager, L10nStatistics $statistics, ) { parent::__construct( $configuration, $plugin_id, $plugin_definition, ); $this->routeMatch = $route_match; $this->statistics = $statistics; $this->languageManager = $language_manager; } /** * {@inheritdoc} */ public static function create( ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, ): self { return new static( $configuration, $plugin_id, $plugin_definition, $container->get('current_route_match'), $container->get('language_manager'), $container->get('l10n_community.statistics') ); } /** * {@inheritdoc} */ public function build(): array { /** @var \Drupal\group\Entity\Group $group */ $group = $this->routeMatch->getParameter('group'); if (!$group instanceof Group) { return []; } $language = $this->languageManager->getLanguage( $group->get('field_translation_language')->first()->getValue()['target_id'] ); $people = $this->statistics->getPeopleStringCount($language->getId()); $rows = []; foreach ($people as $key => $translator) { $rows[] = [ 'data' => [ 'translator' => Link::createFromRoute($translator->name, 'entity.user.canonical', ['user' => $translator->uid]), 'count' => $translator->sum, ], 'class' => (($key + 1) % 2 === 0) ? 'even' : 'odd', ]; } if (empty($rows)) { return [ '#markup' => '<p>' . $this->t('Nobody contributed to the @name translation yet.', [ '@name' => $language->getName(), ]) . '</p>', ]; } return [ '#type' => 'table', '#header' => [ $this->t('Name'), $this->t('Translation count'), ], '#rows' => $rows, ]; } /** * {@inheritdoc} */ protected function blockAccess(AccountInterface $account): AccessResultInterface { return AccessResult::allowedIf( $account->hasPermission('access localization community') ); } /** * {@inheritdoc} */ public function getCacheContexts(): array { return Cache::mergeContexts( parent::getCacheContexts(), ['url.path'] ); } }