Date.php in Display Suite 8.3
File
src/Plugin/DsField/Date.php
View source
<?php
namespace Drupal\ds\Plugin\DsField;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class Date extends DsFieldBase {
protected $entityTypeManager;
protected $dateFormatter;
protected $time;
public function __construct($configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, DateFormatterInterface $date_formatter, TimeInterface $time) {
$this->entityTypeManager = $entity_type_manager;
$this->dateFormatter = $date_formatter;
$this->time = $time;
parent::__construct($configuration, $plugin_id, $plugin_definition);
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_type.manager'), $container
->get('date.formatter'), $container
->get('datetime.time'));
}
public function build() {
$field = $this
->getFieldConfiguration();
$date_format = str_replace('ds_post_date_', '', $field['formatter']);
$render_key = $this
->getRenderKey();
return [
'#markup' => $this->dateFormatter
->format($this
->entity()->{$render_key}->value, $date_format),
];
}
public function formatters() {
$date_types = $this->entityTypeManager
->getStorage('date_format')
->loadMultiple();
$date_formatters = [];
foreach ($date_types as $machine_name => $entity) {
if ($entity
->isLocked()) {
continue;
}
$date_formatters['ds_post_date_' . $machine_name] = $entity
->label() . ' (' . $this->dateFormatter
->format($this->time
->getRequestTime(), $entity
->id()) . ')';
}
return $date_formatters;
}
public function getRenderKey() {
return '';
}
}
Classes
Name |
Description |
Date |
The base plugin to create DS post date plugins. |