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