Commit 12ee21a3 authored by webchick's avatar webchick

Issue #2328061 by tim.plunkett: Move datetime's FormElement #type classes in Core.

parent 3467f054
......@@ -382,6 +382,26 @@ core.base_field_override.*.*.*:
type: field_config_base
label: 'Base field bundle override'
core.date_format.*:
type: config_entity
label: 'Date format'
mapping:
id:
type: string
label: 'ID'
label:
type: label
label: 'Label'
locked:
type: boolean
label: 'Locked'
pattern:
type: date_format
label: 'PHP date format'
langcode:
type: string
label: 'Default language'
# Schema for the String field type.
field.string.instance_settings:
......
......@@ -843,6 +843,68 @@ function template_preprocess_time(&$variables) {
$variables['attributes'] = new Attribute($variables['attributes']);
}
/**
* Prepares variables for datetime form element templates.
*
* The datetime form element serves as a wrapper around the date element type,
* which creates a date and a time component for a date.
*
* Default template: datetime-form.html.twig.
*
* @param array $variables
* An associative array containing:
* - element: An associative array containing the properties of the element.
* Properties used: #title, #value, #options, #description, #required,
* #attributes.
*
* @see form_process_datetime()
*/
function template_preprocess_datetime_form(&$variables) {
$element = $variables['element'];
$variables['attributes'] = array();
if (isset($element['#id'])) {
$variables['attributes']['id'] = $element['#id'];
}
if (!empty($element['#attributes']['class'])) {
$variables['attributes']['class'] = (array) $element['#attributes']['class'];
}
$variables['attributes']['class'][] = 'container-inline';
$variables['content'] = $element;
}
/**
* Prepares variables for datetime form wrapper templates.
*
* Default template: datetime-wrapper.html.twig.
*
* @param array $variables
* An associative array containing:
* - element: An associative array containing the properties of the element.
* Properties used: #title, #children, #required, #attributes.
*/
function template_preprocess_datetime_wrapper(&$variables) {
$element = $variables['element'];
if (!empty($element['#title'])) {
$variables['title'] = $element['#title'];
}
if (!empty($element['#description'])) {
$variables['description'] = $element['#description'];
}
$title_attributes = array('class' => array('label'));
// For required datetime fields a 'form-required' class is appended to the
// label attributes.
if (!empty($element['#required'])) {
$title_attributes['class'][] = 'form-required';
}
$variables['title_attributes'] = new Attribute($title_attributes);
$variables['content'] = $element['#children'];
}
/**
* Prepares variables for status message templates.
*
......@@ -2183,6 +2245,14 @@ function drupal_common_theme() {
'variables' => array('timestamp' => NULL, 'text' => NULL, 'attributes' => array(), 'html' => FALSE),
'template' => 'time',
),
'datetime_form' => array(
'template' => 'datetime-form',
'render element' => 'element',
),
'datetime_wrapper' => array(
'template' => 'datetime-wrapper',
'render element' => 'element',
),
'status_messages' => array(
'variables' => array('display' => NULL),
'template' => 'status-messages',
......
......@@ -2,10 +2,10 @@
/**
* @file
* Contains \Drupal\system\DateFormatInterface.
* Contains \Drupal\Core\Datetime\DateFormatInterface.
*/
namespace Drupal\system;
namespace Drupal\Core\Datetime;
use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\Core\Datetime\DrupalDateTime;
......
<?php
/**
* @file
* Contains \Drupal\datetime\DateHelper.
* Contains \Drupal\Core\Datetime\DateHelper.
*
* Lots of helpful functions for use in massaging dates, specific to the the
* Gregorian calendar system. The values include both translated and
......@@ -14,7 +14,7 @@
* translation should be hard-coded and wrapped in t() so the translation system
* will be able to process them.
*/
namespace Drupal\datetime;
namespace Drupal\Core\Datetime;
use Drupal\Core\Datetime\DrupalDateTime;
......
<?php
/**
* @file
* Contains \Drupal\Core\Datetime\Element\DateElementBase.
*/
namespace Drupal\Core\Datetime\Element;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\Render\Element\FormElement;
/**
* Provides a base class for date elements.
*/
abstract class DateElementBase extends FormElement {
/**
* Specifies the start and end year to use as a date range.
*
* Handles a string like -3:+3 or 2001:2010 to describe a dynamic range of
* minimum and maximum years to use in a date selector.
*
* Centers the range around the current year, if any, but expands it far enough
* so it will pick up the year value in the field in case the value in the field
* is outside the initial range.
*
* @param string $string
* A min and max year string like '-3:+1' or '2000:2010' or '2000:+3'.
* @param object $date
* (optional) A date object to test as a default value. Defaults to NULL.
*
* @return array
* A numerically indexed array, containing the minimum and maximum year
* described by this pattern.
*/
protected static function datetimeRangeYears($string, $date = NULL) {
$this_year = date_format(new DrupalDateTime(), 'Y');
list($min_year, $max_year) = explode(':', $string);
// Valid patterns would be -5:+5, 0:+1, 2008:2010.
$plus_pattern = '@[\+|\-][0-9]{1,4}@';
$year_pattern = '@^[0-9]{4}@';
if (!preg_match($year_pattern, $min_year, $matches)) {
if (preg_match($plus_pattern, $min_year, $matches)) {
$min_year = $this_year + $matches[0];
}
else {
$min_year = $this_year;
}
}
if (!preg_match($year_pattern, $max_year, $matches)) {
if (preg_match($plus_pattern, $max_year, $matches)) {
$max_year = $this_year + $matches[0];
}
else {
$max_year = $this_year;
}
}
// We expect the $min year to be less than the $max year. Some custom values
// for -99:+99 might not obey that.
if ($min_year > $max_year) {
$temp = $max_year;
$max_year = $min_year;
$min_year = $temp;
}
// If there is a current value, stretch the range to include it.
$value_year = $date instanceOf DrupalDateTime ? $date->format('Y') : '';
if (!empty($value_year)) {
$min_year = min($value_year, $min_year);
$max_year = max($value_year, $max_year);
}
return array($min_year, $max_year);
}
}
This diff is collapsed.
This diff is collapsed.
......@@ -2,15 +2,14 @@
/**
* @file
* Contains \Drupal\system\Entity\DateFormat.
* Contains \Drupal\Core\Datetime\Entity\DateFormat.
*/
namespace Drupal\system\Entity;
namespace Drupal\Core\Datetime\Entity;
use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\system\DateFormatInterface;
use Drupal\Core\Datetime\DateFormatInterface;
/**
* Defines the Date Format configuration entity class.
......@@ -20,22 +19,12 @@
* label = @Translation("Date format"),
* handlers = {
* "access" = "Drupal\system\DateFormatAccessControlHandler",
* "list_builder" = "Drupal\system\DateFormatListBuilder",
* "form" = {
* "add" = "Drupal\system\Form\DateFormatAddForm",
* "edit" = "Drupal\system\Form\DateFormatEditForm",
* "delete" = "Drupal\system\Form\DateFormatDeleteForm"
* }
* },
* entity_keys = {
* "id" = "id",
* "label" = "label"
* },
* admin_permission = "administer site configuration",
* links = {
* "delete-form" = "entity.date_format.delete_form",
* "edit-form" = "entity.date_format.edit_form"
* }
* )
*/
class DateFormat extends ConfigEntityBase implements DateFormatInterface {
......
@todo This must be here because DrupalKernel will only allow namespaces to
provide plugins if there is a Plugin subdirectory, and git does not allow
empty subdirectories. This file should be removed once
https://www.drupal.org/node/2309889 is fixed.
......@@ -397,15 +397,15 @@ public function testDateFormatTranslation() {
// Update translatable fields.
$edit = array(
'config_names[system.date_format.' . $id . '][label][translation]' => $id . ' - FR',
'config_names[system.date_format.' . $id . '][pattern][translation]' => 'D',
'config_names[core.date_format.' . $id . '][label][translation]' => $id . ' - FR',
'config_names[core.date_format.' . $id . '][pattern][translation]' => 'D',
);
// Save language specific version of form.
$this->drupalPostForm($translation_page_url, $edit, t('Save translation'));
// Get translation and check we've got the right value.
$override = \Drupal::languageManager()->getLanguageConfigOverride('fr', 'system.date_format.' . $id);
$override = \Drupal::languageManager()->getLanguageConfigOverride('fr', 'core.date_format.' . $id);
$expected = array(
'label' => $id . ' - FR',
'pattern' => 'D',
......
This diff is collapsed.
......@@ -110,7 +110,7 @@ public function testConfigTranslation() {
);
$this->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));
$wrapper = $this->container->get('locale.config.typed')->get('system.date_format.medium');
$wrapper = $this->container->get('locale.config.typed')->get('core.date_format.medium');
// Get translation and check we've only got the site name.
$translation = $wrapper->getTranslation($langcode);
......
......@@ -19,7 +19,7 @@ class EntityDateFormat extends EntityConfigBase {
/**
* {@inheritdoc}
*
* @param \Drupal\system\DateFormatInterface $entity
* @param \Drupal\Core\Datetime\DateFormatInterface $entity
* The date entity.
*/
protected function updateEntityProperty(EntityInterface $entity, array $parents, $value) {
......
......@@ -9,7 +9,7 @@
use Drupal\Core\Database\Database;
/**
* Upgrade date formats to system.date_format.*.yml.
* Upgrade date formats to core.date_format.*.yml.
*
* @group migrate_drupal
*/
......
......@@ -114,26 +114,6 @@ system.date:
type: boolean
label: 'Remind users at login if their time zone is not set'
system.date_format.*:
type: config_entity
label: 'Date format'
mapping:
id:
type: string
label: 'ID'
label:
type: label
label: 'Label'
locked:
type: boolean
label: 'Locked'
pattern:
type: date_format
label: 'PHP date format'
langcode:
type: string
label: 'Default language'
system.diff:
type: mapping
label: 'Diff settings'
......
......@@ -186,14 +186,14 @@ function testDisableEnable() {
$this->themeHandler()->disable(array($name));
$this->assertIdentical($this->config("$name.settings")->get('base'), 'only');
$this->assertIdentical($this->config('system.date_format.fancy')->get('label'), 'Fancy date');
$this->assertIdentical($this->config('core.date_format.fancy')->get('label'), 'Fancy date');
// Default configuration never overwrites custom configuration, so just
// changing values in existing configuration will cause ConfigInstaller to
// simply skip those files. To ensure that no default configuration is
// re-imported, the custom configuration has to be deleted.
$this->configStorage()->delete("$name.settings");
$this->configStorage()->delete('system.date_format.fancy');
$this->configStorage()->delete('core.date_format.fancy');
// Reflect direct storage operations in ConfigFactory.
$this->container->get('config.factory')->reset();
......@@ -206,7 +206,7 @@ function testDisableEnable() {
$this->assertEqual(array_keys(system_list('theme')), array_keys($themes));
$this->assertFalse($this->config("$name.settings")->get());
$this->assertNull($this->config('system.date_format.fancy')->get('label'));
$this->assertNull($this->config('core.date_format.fancy')->get('label'));
}
/**
......
......@@ -1277,6 +1277,20 @@ function system_retrieve_file($url, $destination = NULL, $managed = FALSE, $repl
return $local;
}
/**
* Implements hook_entity_type_build().
*/
function system_entity_type_build(array &$entity_types) {
/** @var $entity_types \Drupal\Core\Entity\EntityTypeInterface[] */
$entity_types['date_format']
->setFormClass('add', 'Drupal\system\Form\DateFormatAddForm')
->setFormClass('edit', 'Drupal\system\Form\DateFormatEditForm')
->setFormClass('delete', 'Drupal\system\Form\DateFormatDeleteForm')
->setListBuilderClass('Drupal\system\DateFormatListBuilder')
->setLinkTemplate('edit-form', 'entity.date_format.edit_form')
->setLinkTemplate('delete-form', 'entity.date_format.delete_form');
}
/**
* Implements hook_page_alter().
*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment