l10n_server-2.x-dev/l10n_packager/src/Plugin/Block/TopDownloadsBlock.php

l10n_packager/src/Plugin/Block/TopDownloadsBlock.php
<?php

declare(strict_types=1);

namespace Drupal\l10n_packager\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\Database\Connection;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Routing\CurrentRouteMatch;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Provides a top downloads block.
 *
 * @Block(
 *   id = "l10n_packager_top_downloads",
 *   admin_label = @Translation("Top Downloads"),
 *   category = @Translation("L10n")
 * )
 */
final class TopDownloadsBlock extends BlockBase implements ContainerFactoryPluginInterface {

  /**
   * The current route match.
   *
   * @var \Drupal\Core\Routing\CurrentRouteMatch
   */
  protected CurrentRouteMatch $routeMatch;

  /**
   * The database connection.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected Connection $connection;

  /**
   * Constructs a new TopDownloadsBlock 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\CurrentRouteMatch $route_match
   *   The current route match service.
   * @param \Drupal\Core\Database\Connection $connection
   *   The database connection.
   */
  public function __construct(
    array $configuration,
    $plugin_id,
    $plugin_definition,
    CurrentRouteMatch $route_match,
    Connection $connection,
  ) {
    parent::__construct(
      $configuration,
      $plugin_id,
      $plugin_definition
    );
    $this->routeMatch = $route_match;
    $this->connection = $connection;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition): self {
    return new TopDownloadsBlock(
      $configuration,
      $plugin_id,
      $plugin_definition,
      $container->get('current_route_match'),
      $container->get('database'),
    );
  }

  /**
   * {@inheritdoc}
   */
  public function build(): array {
    /** @var \Drupal\group\Entity\Group $group */
    if (!$group = \Drupal::routeMatch()->getParameter('group')) {
      return [];
    }

    $language = \Drupal::languageManager()->getLanguage(
      $group->get('field_translation_language')->first()->getValue()['target_id']
    );

    // Output the highlighted project first and foremost.
    $select = \Drupal::database()
      ->select('l10n_server_project', 'p')
      ->fields('p', ['title', 'uri', 'pid'])
      ->condition('status', 1)
      ->orderBy('weight')
      ->range(0, 20);

    /** @var \Drupal\l10n_packager\L10nPackager $packager */
    $packager = \Drupal::service('l10n_packager.packager');
    if ($project = $packager->getHighlightedProject()) {
      $rows = $packager->getProjectDownloads($project, $language);
      $build['downloads'] = [
        '#type' => 'table',
        '#caption' => $project->label(),
        '#header' => [
          $this->t('Project'),
          $this->t('Version'),
          $this->t('Downloads'),
          $this->t('Date created'),
          $this->t('Up to date as of'),
        ],
        '#rows' => $rows,
        '#attributes' => [
          'class' => [
            'l10n-packager-download-table',
          ],
        ],
      ];
      $select->condition('pid', $project->id(), '!=');
    }
    $result = $select->execute();

    // Make a list of up and coming 20 projects as well.
    $downloads = [];
    foreach ($result as $row) {
      $project = \Drupal::entityTypeManager()
        ->getStorage('l10n_server_project')
        ->load($row->pid);
      $downloads = array_merge($downloads, $packager->getProjectDownloads($project, $language, TRUE));
    }

    $build['caption'] = [
      '#type' => 'html_tag',
      '#tag' => 'h3',
      '#value' => $this->t('Further top projects'),
    ];

    $build['projects'] = [
      '#type' => 'table',
      '#rows' => $downloads,
      '#attributes' => [
        'class' => [
          'l10n-packager-download-table',
          'l10n-packager-download-dynamic',
        ],
      ],
    ];
    $project_count = \Drupal::entityTypeManager()
      ->getStorage('l10n_server_project')
      ->getQuery()
      ->accessCheck(TRUE)
      ->condition('status', 1)
      ->count()
      ->accessCheck(FALSE)
      ->execute();

    $build['more'] = [
      '#type' => 'html_tag',
      '#tag' => 'p',
      '#value' => $this->formatPlural(
        $project_count,
        '<a href="@download">More downloads</a> are available. These are just the top projects among @count projects translated on this server.',
        '<a href="@download">More downloads</a> are available. These are just the top projects among @count projects translated on this server.',
        ['@download' => Url::fromUri('internal:/translate/downloads')->toString()],
      ),
    ];

    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']
    );
  }

}

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

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