Skip to content

Commit

Permalink
Added 'Month Day, Year' original date display format
Browse files Browse the repository at this point in the history
  • Loading branch information
nick-deer committed Apr 23, 2024
1 parent ad1063c commit c6cd36e
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ content:
weight: 33
region: content
field_original_date:
type: original_date_formatter
type: year_month_day_original_date_formatter
label: above
settings: { }
third_party_settings: { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ content:
weight: 1
region: content
field_original_date:
type: original_date_formatter
type: year_month_day_original_date_formatter
label: hidden
settings: { }
third_party_settings: { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ content:
weight: 33
region: content
field_original_date:
type: original_date_formatter
type: year_month_day_original_date_formatter
label: above
settings: { }
third_party_settings: { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ content:
weight: 33
region: content
field_original_date:
type: original_date_formatter
type: year_month_day_original_date_formatter
label: above
settings: { }
third_party_settings: { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ content:
weight: 1
region: content
field_original_date:
type: original_date_formatter
type: year_month_day_original_date_formatter
label: above
settings: { }
third_party_settings: { }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace Drupal\original_date\Plugin\Field\FieldFormatter;

use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Field\FieldItemListInterface;

/**
* Plugin implementation of the 'textual_month_day_year_original_date_formatter'
* formatter.
*
* @FieldFormatter(
* id = "textual_month_day_year_original_date_formatter",
* module = "original_date",
* description = "Displays original date in Month, Day Year format.",
* label = @Translation("Month Day, Year Original Date Formatter"),
* field_types = {
* "original_date"
* }
* )
*/
class OriginalDateFormatterTextualMonthDayYear extends FormatterBase
{
/**
* {@inheritdoc}
*/
public function settingsSummary()
{
$summary = [];
$summary[] = $this->t('Displays the original date in Month Day, Year format (e.g. January 3, 2015).');
return $summary;
}

/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode)
{
$element = [];
$months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];

foreach ($items as $delta => $item) {
$monthText = $months[intval($item->month) - 1];
$dateTemplate = "@month @day, @year";
$dateStr = $this->t($dateTemplate, [
'@month' => $monthText,
'@day' => $item->day,
'@year' => $item->year
]);
$element[$delta] = ['#markup' => $dateStr];
}

return $element;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,28 @@
use Drupal\Core\Field\FieldItemListInterface;

/**
* Plugin implementation of the 'original_date_formatter' formatter.
* Plugin implementation of the 'year_month_day_original_date_formatter'
* formatter.
*
* @FieldFormatter(
* id = "original_date_formatter",
* id = "year_month_day_original_date_formatter",
* module = "original_date",
* label = @Translation("Original Date Formatter"),
* description = "Displays original date in YYYY-MM-DD format.",
* label = @Translation("YYYY-MM-DD Original Date Formatter"),
* field_types = {
* "original_date"
* }
* )
*/
class OriginalDateFormatter extends FormatterBase
class OriginalDateFormatterYearMonthDay extends FormatterBase
{
/**
* {@inheritdoc}
*/
public function settingsSummary()
{
$summary = [];
$summary[] = $this->t('Displays the original date.');
$summary[] = $this->t('Displays the original date in YYYY-MM-DD format (e.g. 2015-08-20).');
return $summary;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* module = "original_date",
* description = @Translation("Date the item was originally published."),
* default_widget = "original_date_text",
* default_formatter = "original_date_formatter"
* default_formatter = "year_month_day_original_date_formatter"
* )
*/
class OriginalDateField extends FieldItemBase {
Expand Down

0 comments on commit c6cd36e

Please sign in to comment.