View source
<?php
namespace Drupal\scheduled_publish\Plugin\Field\FieldFormatter;
use Drupal\Component\Utility\Html;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\Entity\EntityTypeManager;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Logger\LoggerChannelFactory;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\scheduled_publish\Plugin\Field\FieldType\ScheduledPublish;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ScheduledPublishGenericFormatter extends FormatterBase implements ContainerFactoryPluginInterface {
private $logger;
private $entityTypeManager;
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, LoggerChannelFactory $loggerChannelFactory, EntityTypeManager $entityTypeManager) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
$this->logger = $loggerChannelFactory
->get('scheduled_publish');
$this->entityTypeManager = $entityTypeManager;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($plugin_id, $plugin_definition, $configuration['field_definition'], $configuration['settings'], $configuration['label'], $configuration['view_mode'], $configuration['third_party_settings'], $container
->get('logger.factory'), $container
->get('entity_type.manager'));
}
public static function defaultSettings() {
return [
'date_format' => 'html_datetime',
'text_pattern' => '%moderation_state% - %date%',
] + parent::defaultSettings();
}
public function settingsForm(array $form, FormStateInterface $form_state) : array {
return [
'date_format' => [
'#title' => $this
->t('Date format'),
'#type' => 'select',
'#options' => $this
->getDateFormats(),
'#default_value' => $this
->getSetting('date_format'),
],
'text_pattern' => [
'#title' => $this
->t('Text replace pattern'),
'#type' => 'textfield',
'#default_value' => $this
->getSetting('text_pattern'),
],
] + parent::settingsForm($form, $form_state);
}
public function settingsSummary() {
return [
'#markup' => $this
->t('Displays date in a custom format')
->render(),
];
}
public function viewElements(FieldItemListInterface $items, $langcode) : array {
$elements = [];
$strDateFormat = $this
->getSetting('date_format');
$strTextPattern = $this
->getSetting('text_pattern');
foreach ($items as $delta => $item) {
$rawValue = $item
->getValue();
$dateTime = $rawValue['value'];
$moderationState = $rawValue['moderation_state'];
$elements[$delta] = [
'#markup' => $this
->parseData($dateTime, $strDateFormat, $moderationState, $strTextPattern),
];
}
return $elements;
}
protected function viewValue(FieldItemInterface $item) : string {
return nl2br(Html::escape($item->value));
}
private function getDateFormats() : array {
$formats = [];
$dateFormats = $this->entityTypeManager
->getStorage('date_format')
->loadMultiple();
foreach ($dateFormats as $dateFormat) {
$formats[$dateFormat
->id()] = $dateFormat
->get('label');
}
return $formats;
}
private function parseData(string $strDateTime, string $strDateFormat, string $moderationState, string $pattern) : string {
$date = $this
->parseDate($strDateTime, $strDateFormat);
return str_replace([
'%moderation_state%',
'%date%',
], [
$moderationState,
$date,
], $pattern);
}
private function parseDate(string $strDateTime, string $strDateFormat) : string {
$dateFormat = $this->entityTypeManager
->getStorage('date_format')
->load($strDateFormat);
if ($dateFormat !== NULL) {
$pattern = $dateFormat
->getPattern();
$drupalDateTime = DrupalDateTime::createFromFormat(ScheduledPublish::DATETIME_STORAGE_FORMAT, $strDateTime);
return $drupalDateTime
->format($pattern);
}
$this->logger
->error($this
->t('Date format: "' . $this
->getSetting('date_format') . '" could not be found!'));
return NULL;
}
}