l10n_server-2.x-dev/l10n_community/src/Plugin/Block/ProjectStatisticsBlock.php
l10n_community/src/Plugin/Block/ProjectStatisticsBlock.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\Link; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Routing\CurrentRouteMatch; use Drupal\Core\Session\AccountInterface; use Drupal\Core\Url; use Drupal\l10n_community\L10nStatistics; use Drupal\l10n_server\Entity\L10nServerProjectInterface; use Symfony\Component\DependencyInjection\ContainerInterface; /** * Provides a project statistics block. * * @Block( * id = "l10n_community_project_statistics", * admin_label = @Translation("Project statistics"), * category = @Translation("L10n") * ) */ final class ProjectStatisticsBlock extends BlockBase implements ContainerFactoryPluginInterface { /** * 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\l10n_community\L10nStatistics $statistics * The l10n_community.statistics service. * @param \Drupal\Core\Routing\CurrentRouteMatch $routeMatch * The current_route_match service. */ public function __construct( array $configuration, $plugin_id, $plugin_definition, private readonly L10nStatistics $statistics, private readonly CurrentRouteMatch $routeMatch, ) { parent::__construct($configuration, $plugin_id, $plugin_definition); } /** * {@inheritdoc} */ public static function create( ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, ): self { return new ProjectStatisticsBlock( $configuration, $plugin_id, $plugin_definition, $container->get('l10n_community.statistics'), $container->get('current_route_match') ); } /** * {@inheritdoc} */ public function build(): array { $project = $this->routeMatch->getParameter('l10n_server_project'); if (!$project instanceof L10nServerProjectInterface) { return []; } $num_source = $this->statistics->getProjectStringCount($project); $num_warnings = $this->statistics->getProjectWarningsCount($project); $num_releases = $this->statistics->getProjectReleases($project); $num_parsed = $this->statistics->getProjectReleasesParsed($project); // Build list of links for summary. $items = []; if ($project->getHomepage()) { $items[] = $this->t('Project home: <a href="@project_home">@project_home</a>', [ '@project_name' => $project->label(), '@project_home' => $project->getHomepage(), ]); } if ($num_releases === 0) { // If we don't know of any releases, we do not list source string or // warning information, since we should have all zeros there too. This // summarizes our stand in short, that we do not yet have data here. $items[] = $this->t('No releases known yet'); } else { $items[] = Link::fromTextAndUrl($this->formatPlural($num_parsed, '1 release parsed', '@count releases parsed') . ' (' . $this->formatPlural($num_releases, '1 known', '@count known') . ')', Url::fromUri('internal:/translate/projects/' . $project->id() . '/releases'))->toString(); $items[] = ($num_source == 0 ? $this->t('No source strings found') : $this->formatPlural($num_source, '1 source string in total', '@count source strings in total')); $items[] = ($num_warnings == 0 ? $this->t('No source code warnings') : $this->formatPlural($num_warnings, '1 source code warning', '@count source code warnings')); } $build['content'] = [ '#theme' => 'item_list', '#items' => $items, ]; return $build; } /** * {@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'] ); } }