DateFormatListBuilder.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. namespace Drupal\system;
  3. use Drupal\Core\Config\Entity\ConfigEntityListBuilder;
  4. use Drupal\Core\Datetime\DateFormatterInterface;
  5. use Drupal\Core\Entity\EntityInterface;
  6. use Drupal\Core\Entity\EntityStorageInterface;
  7. use Drupal\Core\Entity\EntityTypeInterface;
  8. use Symfony\Component\DependencyInjection\ContainerInterface;
  9. /**
  10. * Defines a class to build a listing of date format entities.
  11. *
  12. * @see \Drupal\system\Entity\DateFormat
  13. */
  14. class DateFormatListBuilder extends ConfigEntityListBuilder {
  15. /**
  16. * The date formatter service.
  17. *
  18. * @var \Drupal\Core\Datetime\DateFormatterInterface
  19. */
  20. protected $dateFormatter;
  21. /**
  22. * Constructs a new DateFormatListBuilder object.
  23. *
  24. * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
  25. * The entity type definition.
  26. * @param \Drupal\Core\Entity\EntityStorageInterface $storage
  27. * The entity storage class.
  28. * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
  29. * The date formatter service.
  30. */
  31. public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, DateFormatterInterface $date_formatter) {
  32. parent::__construct($entity_type, $storage);
  33. $this->dateFormatter = $date_formatter;
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
  39. return new static(
  40. $entity_type,
  41. $container->get('entity.manager')->getStorage($entity_type->id()),
  42. $container->get('date.formatter')
  43. );
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public function buildHeader() {
  49. $header['label'] = t('Name');
  50. $header['pattern'] = t('Pattern');
  51. return $header + parent::buildHeader();
  52. }
  53. /**
  54. * {@inheritdoc}
  55. */
  56. public function buildRow(EntityInterface $entity) {
  57. $row['label'] = $entity->label();
  58. $row['pattern'] = $this->dateFormatter->format(REQUEST_TIME, $entity->id());
  59. return $row + parent::buildRow($entity);
  60. }
  61. }