diff --git a/core/lib/Drupal/Core/DrupalKernel.php b/core/lib/Drupal/Core/DrupalKernel.php index 8651134..534a597 100644 --- a/core/lib/Drupal/Core/DrupalKernel.php +++ b/core/lib/Drupal/Core/DrupalKernel.php @@ -537,4 +537,12 @@ protected function getModuleNamespaces($moduleFileNames) { protected function registerNamespaces(array $namespaces = array()) { $this->classLoader->addPrefixes($namespaces); } + + public function serialize() { + return serialize(array($this->environment, $this->debug, $this->classLoader, $this->allowDumping)); + } + public function unserialize($data) { + list($environment, $debug, $class_loader, $allow_dumping) = unserialize($data); + $this->__construct($environment, $debug, $class_loader, $allow_dumping); + } } diff --git a/core/modules/system/lib/Drupal/system/Form/DateFormatAddForm.php b/core/modules/system/lib/Drupal/system/Form/DateFormatAddForm.php new file mode 100644 index 0000000..6dec855 --- /dev/null +++ b/core/modules/system/lib/Drupal/system/Form/DateFormatAddForm.php @@ -0,0 +1,93 @@ + '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 PHP manual for available options.', array('@url' => 'http://userguide.icu-project.org/formatparse/datetime')); + } + else { + $description = t('A user-defined date format. See the PHP manual 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' => ' ', + '#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; + } + +} diff --git a/core/modules/system/lib/Drupal/system/Form/DateFormatEditForm.php b/core/modules/system/lib/Drupal/system/Form/DateFormatEditForm.php new file mode 100644 index 0000000..56afe28 --- /dev/null +++ b/core/modules/system/lib/Drupal/system/Form/DateFormatEditForm.php @@ -0,0 +1,131 @@ +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 PHP manual for available options.', array('@url' => 'http://userguide.icu-project.org/formatparse/datetime')); + } + else { + $description = t('A user-defined date format. See the PHP manual 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' => ' ' . $now . '', + '#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; + } + +} diff --git a/core/modules/system/lib/Drupal/system/Form/DateFormatFormBase.php b/core/modules/system/lib/Drupal/system/Form/DateFormatFormBase.php new file mode 100644 index 0000000..eef17d8 --- /dev/null +++ b/core/modules/system/lib/Drupal/system/Form/DateFormatFormBase.php @@ -0,0 +1,71 @@ +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'; + } + +} diff --git a/core/modules/system/system.admin.inc b/core/modules/system/system.admin.inc index 1566c80..50accd6 100644 --- a/core/modules/system/system.admin.inc +++ b/core/modules/system/system.admin.inc @@ -1884,92 +1884,6 @@ function system_date_time_formats() { } /** - * 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 PHP manual for available options.', array('@url' => 'http://userguide.icu-project.org/formatparse/datetime')); - } - else { - $description = t('A user-defined date format. See the PHP manual 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' => ' ' . $now . '', - '#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. */ function system_date_format_exists($candidate_machine_name) { @@ -1980,45 +1894,6 @@ function system_date_format_exists($candidate_machine_name) { } /** - * 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. * * @see locale_menu() diff --git a/core/modules/system/system.module b/core/modules/system/system.module index ce1b39b..e3a3387 100644 --- a/core/modules/system/system.module +++ b/core/modules/system/system.module @@ -889,13 +889,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', @@ -903,7 +900,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', @@ -3711,10 +3708,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: @@ -3722,32 +3716,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'); } /** diff --git a/core/modules/system/system.routing.yml b/core/modules/system/system.routing.yml index 9859d6a..dd0466e 100644 --- a/core/modules/system/system.routing.yml +++ b/core/modules/system/system.routing.yml @@ -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: