l10n_server-2.x-dev/l10n_packager/src/Entity/ListBuilder/L10nPackagerFileListBuilder.php

l10n_packager/src/Entity/ListBuilder/L10nPackagerFileListBuilder.php
<?php

declare(strict_types=1);

namespace Drupal\l10n_packager\Entity\ListBuilder;

use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityListBuilder;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Provides a list controller for the packager file entity type.
 */
final class L10nPackagerFileListBuilder extends EntityListBuilder {

  /**
   * The date formatter service.
   *
   * @var \Drupal\Core\Datetime\DateFormatterInterface
   */
  protected DateFormatterInterface $dateFormatter;

  /**
   * Constructs a new L10nPackagerFileListBuilder object.
   *
   * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
   *   The entity type definition.
   * @param \Drupal\Core\Entity\EntityStorageInterface $storage
   *   The entity storage class.
   * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
   *   The date formatter service.
   */
  public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, DateFormatterInterface $date_formatter) {
    parent::__construct($entity_type, $storage);

    $this->dateFormatter = $date_formatter;
  }

  /**
   * {@inheritdoc}
   */
  public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type): self {
    return new self(
      $entity_type,
      $container->get('entity_type.manager')->getStorage($entity_type->id()),
      $container->get('date.formatter')
    );
  }

  /**
   * {@inheritdoc}
   */
  public function render(): array {
    $build['table'] = parent::render();

    $total = $this->getStorage()
      ->getQuery()
      ->accessCheck(TRUE)
      ->count()
      ->execute();

    $build['summary']['#markup'] = $this->t('Total packager files: <strong>@total</strong>', ['@total' => $total]);

    return $build;
  }

  /**
   * {@inheritdoc}
   */
  public function buildHeader(): array {
    $header['id'] = $this->t('ID');
    $header['filename'] = $this->t('File name');
    $header['release_name'] = $this->t('Release');
    $header['number_of_strings'] = $this->t('Number of strings');
    $header['language'] = $this->t('Language');
    $header['checked'] = $this->t('Checked');

    return $header + parent::buildHeader();
  }

  /**
   * {@inheritdoc}
   */
  public function buildRow(EntityInterface $entity): array {
    /** @var \Drupal\l10n_packager\Entity\L10nPackagerFileInterface $entity */
    $row['id'] = $entity->id();
    $row['filename'] = $entity->getFile()?->getFilename();
    $row['release_name'] = $entity->getRelease()->getTitle();
    $row['number_of_strings'] = $entity->getStringCount();
    $row['language'] = $entity->getLanguage();
    $row['checked'] = $this->dateFormatter->format($entity->getCheckedTime());

    return $row + parent::buildRow($entity);
  }

}

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

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