Skip to content
Snippets Groups Projects
Commit d44b93b3 authored by Shelane French's avatar Shelane French
Browse files

Issue #3221178 by shelane: Create twig filter to display date from raw value

parent 277b2919
Branches 1.0.x
No related tags found
No related merge requests found
services:
date_ap_style.formatter:
class: Drupal\date_ap_style\ApStyleDateFormatter
arguments: ['@language_manager']
arguments: ['@language_manager', '@config.factory']
date_ap_style.twig_extension:
class: Drupal\date_ap_style\TwigExtension\DateApStyle
arguments: ['@date_ap_style.formatter']
tags:
- {name: twig.extension}
......@@ -2,12 +2,13 @@
namespace Drupal\date_ap_style;
use Drupal\Core\Config\ConfigFactory;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
/**
* Class ApStyleDateFormatter.
* Services for formatting date types using AP Style Date rules.
*/
class ApStyleDateFormatter {
......@@ -20,14 +21,24 @@ class ApStyleDateFormatter {
*/
protected $languageManager;
/**
* Configuration object for AP Style settings.
*
* @var \Drupal\Core\Config\ImmutableConfig
*/
protected $config;
/**
* Constructor.
*
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager.
* @param \Drupal\Core\Config\ConfigFactory $configFactory
* Access config objects.
*/
public function __construct(LanguageManagerInterface $language_manager) {
public function __construct(LanguageManagerInterface $language_manager, ConfigFactory $configFactory) {
$this->languageManager = $language_manager;
$this->config = $configFactory->get('date_ap_style.dateapstylesettings');
}
/**
......@@ -63,6 +74,28 @@ class ApStyleDateFormatter {
return $month_format;
}
/**
* Get base default options from config when none are provided.
*
* @return array
* The default config settings.
*/
private function getOptions() {
$base_defaults = [
'always_display_year' => $this->config->get('always_display_year'),
'display_day' => $this->config->get('display_day'),
'use_today' => $this->config->get('use_today'),
'cap_today' => $this->config->get('cap_today'),
'display_time' => $this->config->get('display_time'),
'time_before_date' => $this->config->get('time_before_date'),
'use_all_day' => $this->config->get('use_all_day'),
'display_noon_and_midnight' => $this->config->get('display_noon_and_midnight'),
'capitalize_noon_and_midnight' => $this->config->get('capitalize_noon_and_midnight'),
'timezone' => $this->config->get('timezone'),
];
return $base_defaults;
}
/**
* Format a timestamp to an AP style date format.
*
......@@ -80,6 +113,10 @@ class ApStyleDateFormatter {
* The formatted date string.
*/
public function formatTimestamp($timestamp, array $options = [], $timezone = NULL, $langcode = NULL) {
if (empty($options)) {
$options = $this->getOptions();
}
if (empty($langcode)) {
$langcode = $this->languageManager->getCurrentLanguage()->getId();
}
......@@ -189,6 +226,10 @@ class ApStyleDateFormatter {
* The formatted date string.
*/
public function formatRange(array $timestamps, array $options = [], $timezone = NULL, $langcode = NULL) {
if (empty($options)) {
$options = $this->getOptions();
}
if (empty($langcode)) {
$langcode = $this->languageManager->getCurrentLanguage()->getId();
}
......
......@@ -6,7 +6,7 @@ use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
/**
* Class DateAPStyleSettings.
* Configuration for the default AP Style Date settings.
*/
class DateAPStyleSettings extends ConfigFormBase {
......
<?php
namespace Drupal\date_ap_style\TwigExtension;
use Drupal\date_ap_style\ApStyleDateFormatter;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
/**
* Custom twig filter to display dates as AP Style.
*/
class DateApStyle extends AbstractExtension {
/**
* The date formatter.
*
* @var \Drupal\date_ap_style\ApStyleDateFormatter
*/
protected $apStyleDateFormatter;
/**
* Constructs \Drupal\Core\Template\TwigExtension.
*
* @param \Drupal\date_ap_style\ApStyleDateFormatter $date_formatter
* The date formatter.
*/
public function __construct(ApStyleDateFormatter $date_formatter) {
$this->apStyleDateFormatter = $date_formatter;
}
/**
* Generates a list of all Twig filters that this extension defines.
*
* @return array
* A key/value array that defines custom Twig filters. The key denotes the
* filter name used in the tag, e.g.:
* @code
* {{ foo|testfilter }}
* @endcode
*
* The value is a standard PHP callback that defines what the filter does.
*/
public function getFilters() {
return [
new TwigFilter('ap_style', [
$this->apStyleDateFormatter,
'formatTimestamp',
]),
];
}
}
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