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

Issue #3282696 by sanduhrs: Port of l10n_community: Add block cache context and language detection

parent 8d1725ca
Loading
Loading
Loading
Loading
+22 −2
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@
namespace Drupal\l10n_community\Plugin\Block;

use Drupal\Core\Block\BlockBase;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\l10n_community\L10nStatistics;
use Symfony\Component\DependencyInjection\ContainerInterface;
@@ -75,12 +76,20 @@ class TopContributorsBlock extends BlockBase implements ContainerFactoryPluginIn
   * {@inheritdoc}
   */
  public function build() {
    $people = $this->statistics->getPeopleStringCount('en');
    $current_path = \Drupal::service('path.current')->getPath();
    $path_items = explode('/', $current_path);
    $languages = \Drupal::languageManager()->getLanguages();
    if (!isset($languages[$path_items[1]])) {
      return [];
    }

    $language = $languages[$path_items[1]];
    $people = $this->statistics->getPeopleStringCount($language->getId());

    $rows = [];
    foreach ($people as $translator) {
      $rows[] = [
        $translator['name'], $translator['sum'],
        $translator['name'], $translator['sum'], $language->getId(),
      ];
    }
    if ($rows) {
@@ -89,6 +98,7 @@ class TopContributorsBlock extends BlockBase implements ContainerFactoryPluginIn
        '#header' => [
          $this->t('Name'),
          $this->t('Translation count'),
          $this->t('Langcode'),
        ],
        '#rows' => $rows,
      ];
@@ -101,4 +111,14 @@ class TopContributorsBlock extends BlockBase implements ContainerFactoryPluginIn
    return $build;
  }

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

}