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

l10n_packager/src/Plugin/Block/TranslationDownloadBlock.php
<?php
declare(strict_types=1);

namespace Drupal\l10n_packager\Plugin\Block;

use Drupal\Core\Access\AccessResult;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Cache\Cache;
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 translation download block.
 *
 * @Block(
 *   id = "l10n_packager_translation_download",
 *   admin_label = @Translation("Translation download"),
 *   category = @Translation("L10n")
 * )
 */
class TranslationDownloadBlock extends BlockBase implements ContainerFactoryPluginInterface {

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

  /**
   * 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\Core\Routing\CurrentRouteMatch $route_match
   *   The current_route_match service.
   */
  public function __construct(
      array $configuration,
      $plugin_id,
      $plugin_definition,
      CurrentRouteMatch $route_match
  ) {
    parent::__construct(
      $configuration,
      $plugin_id,
      $plugin_definition
    );
    $this->routeMatch = $route_match;
  }

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

  /**
   * {@inheritdoc}
   */
  public function build() {
    /** @var \Drupal\l10n_server\Entity\L10nServerProjectInterface $project */
    if (!$project = $this->routeMatch->getParameter('l10n_server_project')) {
      return [];
    }

    $build['content'] = [
      '#markup' => '<a href="' . Url::fromUri('internal:/translate/projects/' . $project->getUri() . '/download')->toString() . '" class="link-button"><span>' . $this->t('Download translations') . '</span></a>',
    ];
    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