Unverified Commit bc053b07 authored by Stefan Auditor's avatar Stefan Auditor Committed by Stefan Auditor
Browse files

Issue #3282696 by sanduhrs: Port of l10n_community: Add top contributors block

parent 19ac1126
Loading
Loading
Loading
Loading
+104 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\l10n_community\Plugin\Block;

use Drupal\Core\Block\BlockBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
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 $statistics;

  /**
   * 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.
   */
  public function __construct(
      array $configuration,
      $plugin_id,
      $plugin_definition,
      L10nStatistics $statistics
  ) {
    parent::__construct(
      $configuration,
      $plugin_id,
      $plugin_definition
    );
    $this->statistics = $statistics;
  }

  /**
   * {@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')
    );
  }

  /**
   * {@inheritdoc}
   */
  public function build() {
    $people = $this->statistics->getPeopleStringCount('en');

    $rows = [];
    foreach ($people as $translator) {
      $rows[] = [
        $translator['name'], $translator['sum'],
      ];
    }
    if ($rows) {
      $build['content'] = [
        '#type' => 'table',
        '#header' => [
          $this->t('Name'),
          $this->t('Translation count'),
        ],
        '#rows' => $rows,
      ];
    }
    else {
      $build['content'] = [
        '#markup' => '<p>' . $this->t('Nobody contributed to this translation yet.') . '</p>',
      ];
    }
    return $build;
  }

}