Skip to content
Snippets Groups Projects
Commit db69f405 authored by Gareth Alexander's avatar Gareth Alexander
Browse files

Issue #3311372 by jurgenhaas, the_g_bomb: Make date format configurable

parent 62ff7732
No related branches found
No related tags found
1 merge request!127Issue #3311372 by jurgenhaas, the_g_bomb: Make date format configurable
general_settings:
radio_behavior: simple
date_format: short
context_lines_leading: 1
context_lines_trailing: 1
revision_pager_limit: 50
......
......@@ -70,7 +70,7 @@ abstract class DiffLayoutBase extends PluginBase implements DiffLayoutInterface,
*/
protected function buildRevisionLink(ContentEntityInterface $revision): GeneratedLink {
if ($revision instanceof RevisionLogInterface) {
$revision_date = $this->date->format($revision->getRevisionCreationTime(), 'short');
$revision_date = $this->date->format($revision->getRevisionCreationTime(), $this->configFactory->get('diff.settings')->get('general_settings.date_format') ?? 'short');
return Link::fromTextAndUrl($revision_date, $revision->toUrl('revision'))->toString();
}
return Link::fromTextAndUrl($revision->label(), $revision->toUrl('revision'))->toString();
......@@ -130,7 +130,7 @@ abstract class DiffLayoutBase extends PluginBase implements DiffLayoutInterface,
$revision_link['date'] = [
'#type' => 'link',
'#title' => $this->date->format($revision->getRevisionCreationTime(), 'short'),
'#title' => $this->date->format($revision->getRevisionCreationTime(), $this->configFactory->get('diff.settings')->get('general_settings.date_format') ?? 'short'),
'#url' => $revision->toUrl('revision'),
'#prefix' => '<div class="diff-revision__item diff-revision__item-date">',
'#suffix' => '</div>',
......
......@@ -73,6 +73,17 @@ class GeneralSettingsForm extends ConfigFormBase {
'#description' => $this->t('<em>Simple exclusion</em> means that users will not be able to select the same revision, <em>Linear restrictions</em> means that users can only select older or newer revisions of the current selections.'),
];
$date_formats = [];
foreach (\Drupal::entityTypeManager()->getStorage('date_format')->loadMultiple() as $machine_name => $value) {
$date_formats[$machine_name] = $value->label();
}
$form['date_format'] = array(
'#type' => 'select',
'#title' => $this->t('Date format'),
'#default_value' => $config->get('general_settings.date_format'),
'#options' => $date_formats,
);
$layout_plugins = $this->diffLayoutManager->getDefinitions();
$weight = \count($layout_plugins) + 1;
$layout_plugins_order = [];
......@@ -235,6 +246,7 @@ class GeneralSettingsForm extends ConfigFormBase {
$keys = [
'radio_behavior',
'date_format',
'context_lines_leading',
'context_lines_trailing',
'layout_plugins',
......
......@@ -155,7 +155,7 @@ class RevisionOverviewForm extends FormBase {
'#theme' => 'username',
'#account' => $revision->getRevisionUser(),
];
$revision_date = $this->date->format($revision->getRevisionCreationTime(), 'short');
$revision_date = $this->date->format($revision->getRevisionCreationTime(), $this->configFactory->get('diff.settings')->get('general_settings.date_format') ?? 'short');
// Use revision link to link to revisions that are not active.
if ($vid != $node->getRevisionId()) {
$link = Link::fromTextAndUrl($revision_date, new Url('entity.node.revision', ['node' => $node->id(), 'node_revision' => $vid]));
......
<?php
namespace Drupal\diff\Plugin\diff\Field;
use Drupal\Core\Datetime\DateFormatter;
use Drupal\diff\FieldDiffBuilderBase;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Plugin to compare date fields.
*
* @FieldDiffBuilder(
* id = "date_field_diff_builder",
* label = @Translation("Date Field Diff"),
* field_types = {
* "changed",
* "created",
* "datetime",
* "daterange",
* "timestamp"
* },
* )
*/
class DateFieldBuilder extends FieldDiffBuilderBase {
/**
* The date formatter service.
*
* @var \Drupal\Core\Datetime\DateFormatter
*/
protected DateFormatter $date;
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
/** @var \Drupal\diff\Plugin\diff\Field\DateFieldBuilder $instance */
$instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);
$instance->date = $container->get('date.formatter');
return $instance;
}
/**
* {@inheritdoc}
*/
public function build(FieldItemListInterface $field_items) {
$result = array();
// Every item from $field_items is of type FieldItemInterface.
foreach ($field_items as $field_key => $field_item) {
if (!$field_item->isEmpty()) {
foreach ($field_item->getValue() as $value) {
if (!is_numeric($value)) {
$value = (new \DateTime($value))->getTimestamp();
}
$result[$field_key][] = $this->date->format((int) $value, $this->configuration['format']);
}
}
}
return $result;
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$date_formats = [];
foreach (\Drupal::entityTypeManager()->getStorage('date_format')->loadMultiple() as $machine_name => $value) {
$date_formats[$machine_name] = $value->label();
}
$form['format'] = array(
'#type' => 'select',
'#title' => $this->t('Format'),
'#default_value' => $this->configuration['format'],
'#options' => $date_formats,
);
return parent::buildConfigurationForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$this->configuration['format'] = $form_state->getValue('format');
parent::submitConfigurationForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
$default_configuration = array(
'format' => 'short',
);
$default_configuration += parent::defaultConfiguration();
return $default_configuration;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment