Commit 291689a5 authored by alexpott's avatar alexpott
Browse files

Issue #2003942 by tim.plunkett: Convert system_configure_date_formats_form() to a controller.

parent d1a88e91
<?php
/**
* @file
* Contains \Drupal\system\Form\DateFormatAddForm.
*/
namespace Drupal\system\Form;
/**
* Provides an add form for date formats.
*/
class DateFormatAddForm extends DateFormatFormBase {
/**
* {@inheritdoc}
*/
public function getFormID() {
return 'date_format_add';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, array &$form_state) {
form_load_include($form_state, 'admin.inc', 'system');
$form['date_format_name'] = array(
'#type' => 'textfield',
'#title' => 'Name',
'#maxlength' => 100,
'#description' => t('Name of the date format'),
'#default_value' => '',
);
$form['date_format_id'] = array(
'#type' => 'machine_name',
'#title' => t('Machine-readable name'),
'#description' => t('A unique machine-readable name. Can only contain lowercase letters, numbers, and underscores.'),
'#default_value' => '',
'#machine_name' => array(
'exists' => 'system_date_format_exists',
'source' => array('date_format_name'),
),
);
if (class_exists('intlDateFormatter')) {
$description = t('A user-defined date format. See the <a href="@url">PHP manual</a> for available options.', array('@url' => 'http://userguide.icu-project.org/formatparse/datetime'));
}
else {
$description = t('A user-defined date format. See the <a href="@url">PHP manual</a> for available options.', array('@url' => 'http://php.net/manual/function.date.php'));
}
$form['date_format_pattern'] = array(
'#type' => 'textfield',
'#title' => t('Format string'),
'#maxlength' => 100,
'#description' => $description,
'#default_value' => '',
'#field_suffix' => ' <small id="edit-date-format-suffix"></small>',
'#ajax' => array(
'callback' => 'system_date_time_lookup',
'event' => 'keyup',
'progress' => array('type' => 'throbber', 'message' => NULL),
),
'#required' => TRUE,
);
$languages = language_list();
$options = array();
foreach ($languages as $langcode => $data) {
$options[$langcode] = $data->name;
}
if (!empty($options)) {
$form['date_langcode'] = array(
'#title' => t('Select localizations'),
'#type' => 'select',
'#options' => $options,
'#multiple' => TRUE,
'#default_value' => '',
);
}
$form['actions'] = array('#type' => 'actions');
$form['actions']['update'] = array(
'#type' => 'submit',
'#value' => t('Add format'),
);
return $form;
}
}
<?php
/**
* @file
* Contains \Drupal\system\Form\DateFormatEditForm.
*/
namespace Drupal\system\Form;
use Drupal\Core\Config\ConfigFactory;
use Drupal\Core\ControllerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides an edit form for date formats.
*/
class DateFormatEditForm extends DateFormatFormBase implements ControllerInterface {
/**
* The config object.
*
* @var \Drupal\Core\Config\Config
*/
protected $config;
/**
* Constructs a new date format form.
*
* @param \Drupal\Core\Config\ConfigFactory $config_factory
* The config factory object.
*/
public function __construct(ConfigFactory $config_factory) {
parent::__construct();
$this->config = $config_factory->get('system.date');
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('config.factory')
);
}
/**
* {@inheritdoc}
*/
public function getFormID() {
return 'date_format_edit';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, array &$form_state, $date_format_id = NULL) {
form_load_include($form_state, 'admin.inc', 'system');
$format_info = $this->config->get('formats.' . $date_format_id);
$pattern = $this->patternType ? $format_info['pattern'][$this->patternType] : '';
$form['date_format_name'] = array(
'#type' => 'textfield',
'#title' => 'Name',
'#maxlength' => 100,
'#description' => t('Name of the date format'),
'#default_value' => empty($format_info['name']) ? '' : $format_info['name']
);
$now = t('Displayed as %date', array('%date' => format_date(REQUEST_TIME, $date_format_id)));
$form['date_format_id'] = array(
'#type' => 'machine_name',
'#title' => t('Machine-readable name'),
'#description' => t('A unique machine-readable name. Can only contain lowercase letters, numbers, and underscores.'),
'#disabled' => !empty($date_format_id),
'#default_value' => $date_format_id,
'#machine_name' => array(
'exists' => 'system_date_format_exists',
'source' => array('date_format_name'),
),
);
if (class_exists('intlDateFormatter')) {
$description = t('A user-defined date format. See the <a href="@url">PHP manual</a> for available options.', array('@url' => 'http://userguide.icu-project.org/formatparse/datetime'));
}
else {
$description = t('A user-defined date format. See the <a href="@url">PHP manual</a> for available options.', array('@url' => 'http://php.net/manual/function.date.php'));
}
$form['date_format_pattern'] = array(
'#type' => 'textfield',
'#title' => t('Format string'),
'#maxlength' => 100,
'#description' => $description,
'#default_value' => $pattern,
'#field_suffix' => ' <small id="edit-date-format-suffix">' . $now . '</small>',
'#ajax' => array(
'callback' => 'system_date_time_lookup',
'event' => 'keyup',
'progress' => array('type' => 'throbber', 'message' => NULL),
),
'#required' => TRUE,
);
$languages = language_list();
$options = array();
foreach ($languages as $langcode => $data) {
$options[$langcode] = $data->name;
}
if (!empty($options)) {
$form['date_langcode'] = array(
'#title' => t('Select localizations'),
'#type' => 'select',
'#options' => $options,
'#multiple' => TRUE,
'#default_value' => empty($format_info['locales']) ? '' : $format_info['locales']
);
}
$form['actions'] = array('#type' => 'actions');
$form['actions']['update'] = array(
'#type' => 'submit',
'#value' => t('Save format'),
);
return $form;
}
}
<?php
/**
* @file
* Contains \Drupal\system\Form\DateFormatFormBase.
*/
namespace Drupal\system\Form;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\Form\FormInterface;
use Drupal\Component\Utility\String;
/**
* Provides a base form for date formats.
*/
abstract class DateFormatFormBase implements FormInterface {
/**
* The date pattern type.
*
* @var string
*/
protected $patternType;
/**
* Constructs a new date format form.
*/
public function __construct() {
$date = new DrupalDateTime();
$this->patternType = $date->canUseIntl() ? DrupalDateTime::INTL : DrupalDateTime::PHP;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, array &$form_state) {
$formats = system_get_date_formats();
$format = trim($form_state['values']['date_format_pattern']);
// The machine name field should already check to see if the requested
// machine name is available. Regardless of machine_name or human readable
// name, check to see if the provided pattern exists.
if (!empty($formats) && in_array($format, array_values($formats)) && (!isset($form_state['values']['date_format_id']) || $form_state['values']['date_format_id'] != $formats[$format]['date_format_id'])) {
form_set_error('date_format', t('This format already exists. Enter a unique format string.'));
}
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, array &$form_state) {
$format = array();
$format['name'] = String::checkPlain($form_state['values']['date_format_name']);
$format['pattern'][$this->patternType] = trim($form_state['values']['date_format_pattern']);
$format['locales'] = !empty($form_state['values']['date_langcode']) ? $form_state['values']['date_langcode'] : array();
// Formats created in the UI are not locked.
$format['locked'] = 0;
system_date_format_save($form_state['values']['date_format_id'], $format);
if (!empty($form_state['values']['date_format_id'])) {
drupal_set_message(t('Custom date format updated.'));
}
else {
drupal_set_message(t('Custom date format added.'));
}
$form_state['redirect'] = 'admin/config/regional/date-time/formats';
}
}
......@@ -1833,92 +1833,6 @@ function system_date_time_formats() {
return $build;
}
/**
* Allow users to add additional date formats.
*
* @param string $date_format_id
* (optional) When present, provides the machine name of the date format that
* is being modified. Defaults to an empty string.
*/
function system_configure_date_formats_form($form, &$form_state, $date_format_id = '') {
$formats = system_get_date_formats();
$format_info = config('system.date')->get('formats.' . $date_format_id);
$patterns = $format_info['pattern'];
$date = new DrupalDateTime;
$pattern = $date->canUseIntl() ? $patterns[DrupalDateTime::INTL] : $patterns[DrupalDateTime::PHP];
$form['date_format_name'] = array(
'#type' => 'textfield',
'#title' => 'Name',
'#maxlength' => 100,
'#description' => t('Name of the date format'),
'#default_value' => empty($format_info['name']) ? '' : $format_info['name']
);
$now = !empty($date_format_id) ? t('Displayed as %date', array('%date' => format_date(REQUEST_TIME, $date_format_id))) : '';
$form['date_format_id'] = array(
'#type' => 'machine_name',
'#title' => t('Machine-readable name'),
'#description' => t('A unique machine-readable name. Can only contain lowercase letters, numbers, and underscores.'),
'#disabled' => !empty($date_format_id),
'#default_value' => $date_format_id,
'#machine_name' => array(
'exists' => 'system_date_format_exists',
'source' => array('date_format_name'),
),
);
if (class_exists('intlDateFormatter')) {
$description = t('A user-defined date format. See the <a href="@url">PHP manual</a> for available options.', array('@url' => 'http://userguide.icu-project.org/formatparse/datetime'));
}
else {
$description = t('A user-defined date format. See the <a href="@url">PHP manual</a> for available options.', array('@url' => 'http://php.net/manual/function.date.php'));
}
$form['date_format_pattern'] = array(
'#type' => 'textfield',
'#title' => t('Format string'),
'#maxlength' => 100,
'#description' => $description,
'#default_value' => empty($pattern) ? '' : $pattern,
'#field_suffix' => ' <small id="edit-date-format-suffix">' . $now . '</small>',
'#ajax' => array(
'callback' => 'system_date_time_lookup',
'event' => 'keyup',
'progress' => array('type' => 'throbber', 'message' => NULL),
),
'#required' => TRUE,
);
$languages = language_list();
$options = array();
foreach ($languages as $langcode => $data) {
$options[$langcode] = $data->name;
}
if (!empty($options)) {
$form['date_langcode'] = array(
'#title' => t('Select localizations'),
'#type' => 'select',
'#options' => $options,
'#multiple' => TRUE,
'#default_value' => empty($format_info['locales']) ? '' : $format_info['locales']
);
}
$form['actions'] = array('#type' => 'actions');
$form['actions']['update'] = array(
'#type' => 'submit',
'#value' => (!empty($date_format_id) ? t('Save format') : t('Add format')),
);
$form['#validate'][] = 'system_date_formats_form_validate';
$form['#submit'][] = 'system_date_formats_form_submit';
return $form;
}
/**
* Checks if the chosen machine_name exists or not.
*/
......@@ -1929,45 +1843,6 @@ function system_date_format_exists($candidate_machine_name) {
return FALSE;
}
/**
* Validate date format string submission.
*/
function system_date_formats_form_validate($form, &$form_state) {
$formats = system_get_date_formats();
$format = trim($form_state['values']['date_format_pattern']);
// The machine name field should already check to see if the requested
// machine name is available. Regardless of machine_name or human readable
// name, check to see if the provided pattern exists.
if (!empty($formats) && in_array($format, array_values($formats)) && (!isset($form_state['values']['date_format_id']) || $form_state['values']['date_format_id'] != $formats[$format]['date_format_id'])) {
form_set_error('date_format', t('This format already exists. Enter a unique format string.'));
}
}
/**
* Process date format string submission.
*/
function system_date_formats_form_submit($form, &$form_state) {
$date = new DrupalDateTime;
$pattern_type = $date->canUseIntl() ? DrupalDateTime::INTL : DrupalDateTime::PHP;
$format = array();
$format['name'] = check_plain($form_state['values']['date_format_name']);
$format['pattern'][$pattern_type] = trim($form_state['values']['date_format_pattern']);
$format['locales'] = !empty($form_state['values']['date_langcode']) ? $form_state['values']['date_langcode'] : array();
// Formats created in the UI are not locked.
$format['locked'] = 0;
system_date_format_save($form_state['values']['date_format_id'], $format);
if (!empty($form_state['values']['date_format_id'])) {
drupal_set_message(t('Custom date format updated.'));
}
else {
drupal_set_message(t('Custom date format added.'));
}
$form_state['redirect'] = 'admin/config/regional/date-time/formats';
}
/**
* Page callback: Displays edit date format links for each language.
*
......
......@@ -876,13 +876,10 @@ function system_menu() {
'title' => 'Add format',
'description' => 'Allow users to add additional date formats.',
'type' => MENU_LOCAL_ACTION,
'page callback' => 'drupal_get_form',
'page arguments' => array('system_configure_date_formats_form'),
'access arguments' => array('administer site configuration'),
'route_name' => 'date_format_add',
'weight' => -10,
'file' => 'system.admin.inc',
);
$items['admin/config/regional/date-time/formats/%system_date_format/edit'] = array(
$items['admin/config/regional/date-time/formats/%/edit'] = array(
'title' => 'Edit date format',
'description' => 'Allow users to edit a configured date format.',
'page callback' => 'drupal_get_form',
......@@ -890,7 +887,7 @@ function system_menu() {
'access arguments' => array('administer site configuration'),
'file' => 'system.admin.inc',
);
$items['admin/config/regional/date-time/formats/%system_date_format/delete'] = array(
$items['admin/config/regional/date-time/formats/%/delete'] = array(
'title' => 'Delete date format',
'description' => 'Allow users to delete a configured date format.',
'route_name' => 'date_format_delete',
......@@ -3701,10 +3698,7 @@ function system_run_automated_cron() {
/**
* Gets the list of defined date formats and attributes.
*
* @param $date_format_id
* (optional) The date type name.
*
* @return
* @return array
* An associative array of date formats. The top-level keys are the
* machine-readable names of the date formats. The values are associative
* arrays with the following keys:
......@@ -3712,32 +3706,9 @@ function system_run_automated_cron() {
* - pattern: An associative array of patterns that will modify the format
* of the date, keyed with 'php' for normal PHP date pattern and 'intl'
* for the alternate pattern used by the IntlDateFormatter.
*
* If $date_format_id was defined, only the date formats associated with the
* given machine name are returned, in an associative array keyed by format
* string.
*/
function system_get_date_formats($date_format_id = NULL) {
$date_formats = config('system.date')->get('formats');
// Return either the specific format or all formats.
return empty($date_format_id) ? $date_formats : $date_formats[$date_format_id];
}
/**
* Menu loader function for a date format.
*
* @param $date_format_id
* The date type name.
*
* @return
* The date format id if it exists and is not locked, FALSE otherwise.
*/
function system_date_format_load($date_format_id) {
if (($format = system_get_date_formats($date_format_id)) && empty($format['locked'])) {
return $date_format_id;
}
return FALSE;
function system_get_date_formats() {
return config('system.date')->get('formats');
}
/**
......
......@@ -74,6 +74,20 @@ system_site_maintenance_mode:
requirements:
_permission: 'administer site configuration'
date_format_add:
pattern: '/admin/config/regional/date-time/formats/add'
defaults:
_form: '\Drupal\system\Form\DateFormatAddForm'
requirements:
_permission: 'administer site configuration'
date_format_edit:
pattern: '/admin/config/regional/date-time/formats/{date_format_id}/edit'
defaults:
_form: '\Drupal\system\Form\DateFormatEditForm'
requirements:
_permission: 'administer site configuration'
date_format_delete:
pattern: 'admin/config/regional/date-time/formats/{format_id}/delete'
defaults:
......
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