l10n_server-2.x-dev/l10n_community/src/Plugin/Block/LanguageStatisticsBlock.php

l10n_community/src/Plugin/Block/LanguageStatisticsBlock.php
<?php

declare(strict_types=1);

namespace Drupal\l10n_community\Plugin\Block;

use Drupal\Core\Access\AccessResult;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Language\LanguageManager;
use Drupal\Core\Path\CurrentPathStack;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\group\Entity\Group;
use Drupal\l10n_community\L10nStatistics;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Provides a language statistics block.
 *
 * @Block(
 *   id = "l10n_community_language_statistics",
 *   admin_label = @Translation("Language statistics"),
 *   category = @Translation("L10n")
 * )
 */
class LanguageStatisticsBlock extends BlockBase implements ContainerFactoryPluginInterface {

  /**
   * The l10n_community.statistics service.
   *
   * @var \Drupal\l10n_community\L10nStatistics
   */
  protected $statistics;

  /**
   * The language manager service.
   *
   * @var \Drupal\Core\Language\LanguageManager
   */
  protected LanguageManager $languageManager;

  /**
   * The current path stack service.
   *
   * @var \Drupal\Core\Path\CurrentPathStack
   */
  protected CurrentPathStack $currentPath;

  /**
   * 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.
   * @param \Drupal\Core\Language\LanguageManager $language_manager
   *   The language manager service.
   * @param \Drupal\Core\Path\CurrentPathStack $current_path
   *   The current path stack service.
   */
  public function __construct(
    array $configuration,
    $plugin_id,
    $plugin_definition,
    L10nStatistics $statistics,
    LanguageManager $language_manager,
    CurrentPathStack $current_path,
  ) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->statistics = $statistics;
    $this->languageManager = $language_manager;
    $this->currentPath = $current_path;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(
    ContainerInterface $container,
    array $configuration,
    $plugin_id,
    $plugin_definition,
  ) {
    return new static(
      $configuration,
      $plugin_id,
      $plugin_definition,
      $container->get('l10n_community.statistics'),
      $container->get('language_manager'),
      $container->get('path.current')
    );
  }

  /**
   * {@inheritdoc}
   */
  public function build() {
    /** @var \Drupal\group\Entity\Group $group */
    $group = \Drupal::routeMatch()->getParameter('group');
    if (!$group instanceof Group) {
      return;
    }
    $langcode = $group->get('field_translation_language')->first()->getValue()['target_id'];
    $language = \Drupal::languageManager()->getLanguage($langcode);

    // Provide a summary of the activity.
    $stats_numbers = $this->statistics->getLanguagesStatistics($language->getId());

    $items = [
      $this->formatPlural($stats_numbers['users'][$langcode]['count'] ?? 0, '1 contributor', '@count contributors'),
      $this->formatPlural($stats_numbers['strings'], '1 string to translate', '@count strings to translate'),
      $this->formatPlural($stats_numbers['translations'][$langcode]['translation_count'], '1 translation recorded', '@count translations recorded'),
      $this->formatPlural($stats_numbers['suggestions'][$langcode]['suggestion_count'], '1 suggestion awaiting approval', '@count suggestions awaiting approval'),
    ];

    $build['content'] = [
      '#theme' => 'item_list',
      '#items' => $items,
    ];
    return $build;
  }

  /**
   * {@inheritdoc}
   */
  protected function blockAccess(AccountInterface $account) {
    return AccessResult::allowedIf(
      $account->hasPermission('access localization community')
    );
  }

  /**
   * {@inheritdoc}
   */
  public function getCacheContexts() {
    return Cache::mergeContexts(
      parent::getCacheContexts(),
      ['url.path']
    );
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc