DateFormatListBuilder.php 2.19 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\system\DateFormatListBuilder.
6 7 8 9
 */

namespace Drupal\system;

10
use Drupal\Core\Config\Entity\ConfigEntityListBuilder;
11
use Drupal\Core\Datetime\Date as DateFormatter;
12
use Drupal\Core\Entity\EntityInterface;
13
use Drupal\Core\Entity\EntityStorageInterface;
14
use Drupal\Core\Entity\EntityTypeInterface;
15 16 17
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
18 19 20
 * Defines a class to build a listing of date format entities.
 *
 * @see \Drupal\system\Entity\DateFormat
21
 */
22
class DateFormatListBuilder extends ConfigEntityListBuilder {
23 24

  /**
25
   * The date formatter service.
26 27 28
   *
   * @var \Drupal\Core\Datetime\Date
   */
29
  protected $dateFormatter;
30 31

  /**
32
   * Constructs a new DateFormatListBuilder object.
33
   *
34 35
   * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
   *   The entity type definition.
36 37
   * @param \Drupal\Core\Entity\EntityStorageInterface $storage
   *   The entity storage class.
38 39
   * @param \Drupal\Core\Datetime\Date $date_formatter
   *   The date formatter service.
40
   */
41
  public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, DateFormatter $date_formatter) {
42
    parent::__construct($entity_type, $storage);
43

44
    $this->dateFormatter = $date_formatter;
45 46 47 48 49
  }

  /**
   * {@inheritdoc}
   */
50
  public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
51
    return new static(
52
      $entity_type,
53
      $container->get('entity.manager')->getStorage($entity_type->id()),
54 55 56 57 58 59 60 61 62 63 64
      $container->get('date')
    );
  }

  /**
   * {@inheritdoc}
   */
  public function buildHeader() {
    $header['id'] = t('Machine name');
    $header['label'] = t('Name');
    $header['pattern'] = t('Pattern');
65
    return $header + parent::buildHeader();
66 67 68 69 70 71
  }

  /**
   * {@inheritdoc}
   */
  public function buildRow(EntityInterface $entity) {
72 73 74 75 76 77
    if ($entity->isLocked()) {
      $row['id'] =  $this->t('@entity_id (locked)', array('@entity_id' => $entity->id()));
    }
    else {
      $row['id'] = $entity->id();
    }
78
    $row['label'] = $this->getLabel($entity);
79
    $row['pattern'] = $this->dateFormatter->format(REQUEST_TIME, $entity->id());
80
    return $row + parent::buildRow($entity);
81 82 83
  }

}