Commit 06cb438e authored by alexpott's avatar alexpott

Issue #2003892 by tim.plunkett, vijaycs85: Convert date formats to config entities.

parent b7f29c4c
......@@ -490,7 +490,7 @@ services:
arguments: ['@module_handler']
date:
class: Drupal\Core\Datetime\Date
arguments: ['@config.factory', '@language_manager']
arguments: ['@plugin.manager.entity', '@language_manager']
feed.bridge.reader:
class: Drupal\Component\Bridge\ZfExtensionManagerSfContainer
calls:
......
......@@ -2,14 +2,14 @@
/**
* @file
* Contains \Drupal\Component\Datetime\Date.
* Contains \Drupal\Core\Datetime\Date.
*/
namespace Drupal\Core\Datetime;
use Drupal\Component\Utility\Xss;
use Drupal\Core\Config\ConfigFactory;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\Entity\EntityManager;
use Drupal\Core\Language\Language;
use Drupal\Core\Language\LanguageManager;
......@@ -26,11 +26,11 @@ class Date {
protected $timezones;
/**
* The config factory.
* The date format storage.
*
* @var \Drupal\Core\Config\ConfigFactory
* @var \Drupal\Core\Entity\EntityStorageControllerInterface
*/
protected $configFactory;
protected $dateFormatStorage;
/**
* Language manager for retrieving the default langcode when none is specified.
......@@ -40,15 +40,15 @@ class Date {
protected $languageManager;
/**
* Constructs a DateFormats object.
* Constructs a Date object.
*
* @param \Drupal\Core\Config\ConfigFactory $config_factory
* The config factory.
* @param \Drupal\Core\Entity\EntityManager $entity_manager
* The entity manager.
* @param \Drupal\Core\Language\LanguageManager $language_manager
* The language manager.
*/
public function __construct(ConfigFactory $config_factory, LanguageManager $language_manager) {
$this->configFactory = $config_factory;
public function __construct(EntityManager $entity_manager, LanguageManager $language_manager) {
$this->dateFormatStorage = $entity_manager->getStorageController('date_format');
$this->languageManager = $language_manager;
}
......@@ -103,14 +103,13 @@ public function format($timestamp, $type = 'medium', $format = '', $timezone = N
$key = $date->canUseIntl() ? DrupalDateTime::INTL : DrupalDateTime::PHP;
// If we have a non-custom date format use the provided date format pattern.
$config = $this->configFactory->get('system.date');
if ($type != 'custom') {
$format = $config->get('formats.' . $type . '.pattern.' . $key);
if ($date_format = $this->dateFormatStorage->load($type)) {
$format = $date_format->getPattern($key);
}
// Fall back to medium if a format was not found.
if (empty($format)) {
$format = $config->get('formats.medium.pattern.' . $key);
$format = $this->dateFormatStorage->load('fallback')->getPattern($key);
}
// Call $date->format().
......
......@@ -32,16 +32,27 @@
function datetime_element_info() {
$format_type = datetime_default_format_type();
$date_format = '';
$time_format = '';
// Date formats cannot be loaded during install or update.
if (!defined('MAINTENANCE_MODE')) {
if ($date_format_entity = entity_load('date_format', 'html_date')) {
$date_format = $date_format_entity->getPattern($format_type);
}
if ($time_format_entity = entity_load('date_format', 'html_time')) {
$time_format = $time_format_entity->getPattern($format_type);
}
}
$types['datetime'] = array(
'#input' => TRUE,
'#element_validate' => array('datetime_datetime_validate'),
'#process' => array('datetime_datetime_form_process'),
'#theme' => 'datetime_form',
'#theme_wrappers' => array('datetime_wrapper'),
'#date_date_format' => config('system.date')->get('formats.html_date.pattern.' . $format_type),
'#date_date_format' => $date_format,
'#date_date_element' => 'date',
'#date_date_callbacks' => array(),
'#date_time_format' => config('system.date')->get('formats.html_time.pattern.' . $format_type),
'#date_time_format' => $time_format,
'#date_time_element' => 'time',
'#date_time_callbacks' => array(),
'#date_year_range' => '1900:2050',
......@@ -429,7 +440,7 @@ function theme_datetime_wrapper($variables) {
* list of the possible formats and HTML5 standards for the HTML5
* requirements. Defaults to the right HTML5 format for the chosen element
* if a HTML5 element is used, otherwise defaults to
* config('system.date')->get('formats.html_date.pattern.php').
* entity_load('date_format', 'html_date')->getPattern().
* - #date_date_element: The date element. Options are:
* - datetime: Use the HTML5 datetime element type.
* - datetime-local: Use the HTML5 datetime-local element type.
......@@ -449,7 +460,7 @@ function theme_datetime_wrapper($variables) {
* a list of the possible formats and HTML5 standards for the HTML5
* requirements. Defaults to the right HTML5 format for the chosen element
* if a HTML5 element is used, otherwise defaults to
* config('system.date')->get('formats.html_time.pattern.php').
* entity_load('date_format', 'html_time')->getPattern().
* - #date_time_callbacks: An array of optional callbacks for the time
* element. Can be used to add a jQuery timepicker or an 'All day' checkbox.
* - #date_year_range: A description of the range of years to allow, like
......@@ -710,11 +721,11 @@ function datetime_html5_format($part, $element) {
case 'date':
switch ($element['#date_date_element']) {
case 'date':
return config('system.date')->get('formats.html_date.pattern.' . $format_type);
return entity_load('date_format', 'html_date')->getPattern($format_type);
case 'datetime':
case 'datetime-local':
return config('system.date')->get('formats.html_datetime.pattern.' . $format_type);
return entity_load('date_format', 'html_datetime')->getPattern($format_type);
default:
return $element['#date_date_format'];
......@@ -724,7 +735,7 @@ function datetime_html5_format($part, $element) {
case 'time':
switch ($element['#date_time_element']) {
case 'time':
return config('system.date')->get('formats.html_time.pattern.' . $format_type);
return entity_load('date_format', 'html_time')->getPattern($format_type);
default:
return $element['#date_time_format'];
......@@ -1140,9 +1151,9 @@ function datetime_form_node_form_alter(&$form, &$form_state, $form_id) {
// Alter the 'Authored on' date to use datetime.
$form['author']['date']['#type'] = 'datetime';
$config = Drupal::config('system.date');
$format = $config->get('formats.html_date.pattern.' . $format_type) . ' ' . $config->get('formats.html_time.pattern.' . $format_type);
$form['author']['date']['#description'] = t('Format: %format. Leave blank to use the time of form submission.', array('%format' => datetime_format_example($format)));
$date_format = entity_load('date_format', 'html_date')->getPattern($format_type);
$time_format = entity_load('date_format', 'html_time')->getPattern($format_type);
$form['author']['date']['#description'] = t('Format: %format. Leave blank to use the time of form submission.', array('%format' => datetime_format_example($date_format . ' ' . $time_format)));
unset($form['author']['date']['#maxlength']);
}
......
......@@ -9,10 +9,14 @@
use Drupal\field\Annotation\FieldFormatter;
use Drupal\Core\Annotation\Translation;
use Drupal\field\Plugin\Type\Formatter\FormatterBase;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Datetime\Date;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\Template\Attribute;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityStorageControllerInterface;
use Drupal\Core\Entity\Field\FieldDefinitionInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\field\Plugin\Type\Formatter\FormatterBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Plugin implementation of the 'datetime_default' formatter.
......@@ -29,7 +33,64 @@
* }
* )
*/
class DateTimeDefaultFormatter extends FormatterBase {
class DateTimeDefaultFormatter extends FormatterBase implements ContainerFactoryPluginInterface {
/**
* The date service.
*
* @var \Drupal\Core\Datetime\Date
*/
protected $dateService;
/**
* The date storage controller.
*
* @var \Drupal\Core\Entity\EntityStorageControllerInterface
*/
protected $dateStorage;
/**
* Constructs a new DateTimeDefaultFormatter.
*
* @param string $plugin_id
* The plugin_id for the formatter.
* @param array $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Entity\Field\FieldDefinitionInterface $field_definition
* The definition of the field to which the formatter is associated.
* @param array $settings
* The formatter settings.
* @param string $label
* The formatter label display setting.
* @param string $view_mode
* The view mode.
* @param \Drupal\Core\Datetime\Date $date_service
* The date service.
* @param \Drupal\Core\Entity\EntityStorageControllerInterface $date_storage
* The date storage controller.
*/
public function __construct($plugin_id, array $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, Date $date_service, EntityStorageControllerInterface $date_storage) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode);
$this->dateService = $date_service;
$this->dateStorage = $date_storage;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, array $plugin_definition) {
return new static(
$plugin_id,
$plugin_definition,
$configuration['field_definition'],
$configuration['settings'],
$configuration['label'],
$configuration['view_mode'],
$container->get('date'),
$container->get('plugin.manager.entity')->getStorageController('date_format')
);
}
/**
* {@inheritdoc}
......@@ -89,22 +150,18 @@ public function viewElements(EntityInterface $entity, $langcode, array $items) {
*/
function dateFormat($date) {
$format_type = $this->getSetting('format_type');
return format_date($date->getTimestamp(), $format_type);
return $this->dateService->format($date->getTimestamp(), $format_type);
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, array &$form_state) {
$element = array();
$time = new DrupalDateTime();
$format_types = system_get_date_formats();
if (!empty($format_types)) {
foreach ($format_types as $type => $type_info) {
$options[$type] = $type_info['name'] . ' (' . format_date($time->format('U'), $type) . ')';
}
$format_types = $this->dateStorage->loadMultiple();
foreach ($format_types as $type => $type_info) {
$format = $this->dateService->format($time->format('U'), $type);
$options[$type] = $type_info->label() . ' (' . $format . ')';
}
$elements['format_type'] = array(
......
......@@ -30,6 +30,13 @@
*/
class DateTimeDefaultWidget extends WidgetBase {
/**
* The date format storage.
*
* @var \Drupal\Core\Entity\EntityStorageControllerInterface
*/
protected $dateStorage;
/**
* {@inheritdoc}
*/
......@@ -41,6 +48,9 @@ public function __construct($plugin_id, array $plugin_definition, FieldDefinitio
$field_definition->default_value_function = $this->defaultValueFunction();
}
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings);
// @todo Inject this once https://drupal.org/node/2035317 is in.
$this->dateStorage = \Drupal::entityManager()->getStorageController('date_format');
}
/**
......@@ -74,7 +84,7 @@ public function formElement(array $items, $delta, array $element, $langcode, arr
case 'date':
$date_type = 'date';
$time_type = 'none';
$date_format = config('system.date')->get('formats.html_date.pattern.' . $format_type);
$date_format = $this->dateStorage->load('html_date')->getPattern($format_type);
$time_format = '';
$element_format = $date_format;
$storage_format = DATETIME_DATE_STORAGE_FORMAT;
......@@ -83,8 +93,8 @@ public function formElement(array $items, $delta, array $element, $langcode, arr
default:
$date_type = 'date';
$time_type = 'time';
$date_format = config('system.date')->get('formats.html_date.pattern.' . $format_type);
$time_format = config('system.date')->get('formats.html_time.pattern.' . $format_type);
$date_format = $this->dateStorage->load('html_date')->getPattern($format_type);
$time_format = $this->dateStorage->load('html_time')->getPattern($format_type);
$element_format = $date_format . ' ' . $time_format;
$storage_format = DATETIME_DATETIME_STORAGE_FORMAT;
break;
......
......@@ -104,8 +104,8 @@ function testDateField() {
$value = '2012-12-31 00:00:00';
$date = new DrupalDateTime($value);
$format_type = $date->canUseIntl() ? DrupalDateTime::INTL : DrupalDateTime::PHP;
$date_format = config('system.date')->get('formats.html_date.pattern.' . $format_type);
$time_format = config('system.date')->get('formats.html_time.pattern.' . $format_type);
$date_format = entity_load('date_format', 'html_date')->getPattern($format_type);
$time_format = entity_load('date_format', 'html_time')->getPattern($format_type);
$edit = array(
'user_id' => 1,
......@@ -175,8 +175,8 @@ function testDatetimeField() {
$value = '2012-12-31 00:00:00';
$date = new DrupalDateTime($value);
$format_type = $date->canUseIntl() ? DrupalDateTime::INTL : DrupalDateTime::PHP;
$date_format = config('system.date')->get('formats.html_date.pattern.' . $format_type);
$time_format = config('system.date')->get('formats.html_time.pattern.' . $format_type);
$date_format = entity_load('date_format', 'html_date')->getPattern($format_type);
$time_format = entity_load('date_format', 'html_time')->getPattern($format_type);
$edit = array(
'user_id' => 1,
......
......@@ -58,6 +58,15 @@ public function __construct(\Traversable $namespaces, CacheBackendInterface $cac
public function createInstance($plugin_id, array $configuration) {
$plugin_definition = $this->discovery->getDefinition($plugin_id);
$plugin_class = DefaultFactory::getPluginClass($plugin_id, $plugin_definition);
// @todo This is copied from \Drupal\Core\Plugin\Factory\ContainerFactory.
// Find a way to restore sanity to
// \Drupal\field\Plugin\Type\Formatter\FormatterBase::__construct().
// If the plugin provides a factory method, pass the container to it.
if (is_subclass_of($plugin_class, 'Drupal\Core\Plugin\ContainerFactoryPluginInterface')) {
return $plugin_class::create(\Drupal::getContainer(), $configuration, $plugin_id, $plugin_definition);
}
return new $plugin_class($plugin_id, $plugin_definition, $configuration['field_definition'], $configuration['settings'], $configuration['label'], $configuration['view_mode']);
}
......
......@@ -948,22 +948,14 @@ function locale_update_8015() {
* Converts localized date formats.
*/
function locale_update_8016() {
$configs = array();
// Fetch all date types from {date_format_type}.
$result = db_query('SELECT * FROM {date_format_locale}');
foreach ($result as $format) {
$language = $format->language;
// Create config objects for the language if not yet done.
if (!isset($configs[$language])) {
$configs[$language] = config('locale.config.' . $language . '.system.date');
}
$configs[$language]->set('formats.' . $format->type . '.pattern.php', $format->format);
}
// Save all instantiated config objects.
foreach ($configs as $config) {
$config->save();
config("locale.config.$language.system.date_format." . $format->type)
->set('pattern.php', $format->format)
->save();
}
}
......
first_day: '0'
country:
default: ''
formats:
long:
name: 'Default Long Date'
pattern:
php: 'l, F j, Y - H:i'
intl: 'EEEE, LLLL d, yyyy - kk:mm'
locked: 0
medium:
name: 'Default Medium Date'
pattern:
php: 'D, m/d/Y - H:i'
intl: 'ccc, MM/dd/yyyy - kk:mm'
locked: 0
short:
name: 'Default Short Date'
pattern:
php: 'm/d/Y - H:i'
intl: 'MM/dd/yyyy - kk:mm'
locked: 0
html_datetime:
name: 'HTML Datetime'
pattern:
php: 'Y-m-d\TH:i:sO'
intl: 'yyyy-MM-dd''T''kk:mm:ssZZ'
locked: 1
html_date:
name: 'HTML Date'
pattern:
php: 'Y-m-d'
intl: 'yyyy-MM-dd'
locked: 1
html_time:
name: 'HTML Time'
pattern:
php: 'H:i:s'
intl: 'H:mm:ss'
locked: 1
html_yearless_date:
name: 'HTML Yearless date'
pattern:
php: 'm-d'
intl: 'MM-d'
locked: 1
html_week:
name: 'HTML Week'
pattern:
php: 'Y-\WW'
intl: 'Y-''W''WW'
locked: 1
html_month:
name: 'HTML Month'
pattern:
php: 'Y-m'
intl: 'Y-MM'
locked: 1
html_year:
name: 'HTML Year'
pattern:
php: 'Y'
intl: 'Y'
locked: 1
id: fallback
uuid: cf0ad071-ded7-4750-b008-8c74f200ff8d
label: 'Fallback date format'
status: '1'
langcode: en
locked: '1'
locales: { }
pattern:
php: 'D, m/d/Y - H:i'
intl: 'ccc, MM/dd/yyyy - kk:mm'
id: html_date
uuid: f4028e7f-3ed8-4737-8f27-39b618214a1f
label: 'HTML Date'
status: '1'
langcode: en
locked: '1'
locales: { }
pattern:
php: Y-m-d
intl: yyyy-MM-dd
id: html_datetime
uuid: bfd7db46-607f-477f-9047-713c312dbb02
label: 'HTML Datetime'
status: '1'
langcode: en
locked: '1'
locales: { }
pattern:
php: 'Y-m-d\TH:i:sO'
intl: 'yyyy-MM-dd''T''kk:mm:ssZZ'
id: html_month
uuid: f85dd1b6-0edb-4024-a800-bb8c87805b2b
label: 'HTML Month'
status: '1'
langcode: en
locked: '1'
locales: { }
pattern:
php: Y-m
intl: Y-MM
id: html_time
uuid: f8e87532-396e-4353-b02c-4493d2031bc6
label: 'HTML Time'
status: '1'
langcode: en
locked: '1'
locales: { }
pattern:
php: 'H:i:s'
intl: 'H:mm:ss'
id: html_week
uuid: d14737ae-d1e0-436b-9225-37113d4c8c7c
label: 'HTML Week'
status: '1'
langcode: en
locked: '1'
locales: { }
pattern:
php: Y-\WW
intl: 'Y-''W''WW'
id: html_year
uuid: a43b8034-a2f2-4507-9400-bed7d389265f
label: 'HTML Year'
status: '1'
langcode: en
locked: '1'
locales: { }
pattern:
php: Y
intl: Y
id: html_yearless_date
uuid: 623ab5ae-ebba-42cc-b100-a4ca10c77b2e
label: 'HTML Yearless date'
status: '1'
langcode: en
locked: '1'
locales: { }
pattern:
php: m-d
intl: MM-d
id: long
uuid: 500ad047-44b4-429f-9b00-d54df9cbaa8c
label: 'Default long date'
status: '1'
langcode: en
locked: '0'
locales: { }
pattern:
php: 'l, F j, Y - H:i'
intl: 'EEEE, LLLL d, yyyy - kk:mm'
id: medium
uuid: 17a8d708-e3b1-432d-9d36-542a97346a60
label: 'Default medium date'
status: '1'
langcode: en
locked: '0'
locales: { }
pattern:
php: 'D, m/d/Y - H:i'
intl: 'ccc, MM/dd/yyyy - kk:mm'
id: short
uuid: a8ee7b86-5ef8-43a5-b200-d2e40e10cd58
label: 'Default short date'
status: '1'
langcode: en
locked: '0'
locales: { }
pattern:
php: 'm/d/Y - H:i'
intl: 'MM/dd/yyyy - kk:mm'
<?php
/**
* @file
* Contains \Drupal\system\DateFormatAccessController.
*/
namespace Drupal\system;
use Drupal\Core\Entity\EntityAccessController;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Session\AccountInterface;
/**
* Provides an access controller for date formats.
*/
class DateFormatAccessController extends EntityAccessController {
/**
* {@inheritdoc}
*/
protected function checkAccess(EntityInterface $entity, $operation, $langcode, AccountInterface $account) {
// There are no restrictions on viewing a date format.
if ($operation == 'view') {
return TRUE;
}
// Locked date formats cannot be updated or deleted.
if (in_array($operation, array('update', 'delete')) && $entity->isLocked()) {
return FALSE;
}
return user_access('administer site configuration', $account);
}
}
<?php
/**
* @file
* Contains \Drupal\system\DateFormatInterface.
*/
namespace Drupal\system;
use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\Core\Datetime\DrupalDateTime;
/**
* Provides an interface defining a date format.
*/
interface DateFormatInterface extends ConfigEntityInterface {
/**
* Gets the date pattern string for this format.
*
* @param string $type
* The date pattern type to set.
*
* @return string
* The pattern string as expected by date().
*/
public function getPattern($type = DrupalDateTime::PHP);
/**
* Sets the date pattern for this format.
*
* @param string $pattern
* The date pattern to use for this format.
* @param string $type
* The date pattern type to set.
*
* @return self
* Returns the date format.
*/
public function setPattern($pattern, $type = DrupalDateTime::PHP);
/**
* Adds a locale for this date format.
*
* @param string $locale
* The locale to add for this format.
*
* @return self
* Returns the date format.
*/
public function addLocale($locale);
/**
* Sets the locales for this date format. This overwrites existing locales.
*
* @param array $locales
* The array of locales to set for this format.
*
* @return self
* Returns the date format.
*/