Newer
Older
Alex Pott
committed
<?php
/**
* @file
* Contains \Drupal\system\Form\DateFormatFormBase.
*/
namespace Drupal\system\Form;
Alex Pott
committed
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\ReplaceCommand;
catch
committed
use Drupal\Core\Config\Entity\ConfigEntityStorageInterface;
Alex Pott
committed
use Drupal\Core\Datetime\Date;
Angie Byron
committed
use Drupal\Core\Language\Language;
Alex Pott
committed
use Symfony\Component\DependencyInjection\ContainerInterface;
Alex Pott
committed
use Drupal\Core\Datetime\DrupalDateTime;
Alex Pott
committed
use Drupal\Core\Entity\EntityFormController;
Alex Pott
committed
/**
Alex Pott
committed
* Provides a base form controller for date formats.
Alex Pott
committed
*/
Alex Pott
committed
abstract class DateFormatFormBase extends EntityFormController {
Alex Pott
committed
/**
* The date pattern type.
*
* @var string
*/
protected $patternType;
Alex Pott
committed
/**
* The date service.
*
* @var \Drupal\Core\Datetime\Date
*/
protected $dateService;
/**
catch
committed
* The date format storage.
*
catch
committed
* @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface
*/
protected $dateFormatStorage;
Alex Pott
committed
/**
* Constructs a new date format form.
Alex Pott
committed
*
* @param \Drupal\Core\Datetime\Date $date_service
* The date service.
catch
committed
* @param \Drupal\Core\Config\Entity\ConfigEntityStorageInterface $date_format_storage
* The date format storage.
Alex Pott
committed
*/
catch
committed
public function __construct(Date $date_service, ConfigEntityStorageInterface $date_format_storage) {
Alex Pott
committed
$date = new DrupalDateTime();
$this->patternType = $date->canUseIntl() ? DrupalDateTime::INTL : DrupalDateTime::PHP;
Alex Pott
committed
$this->dateService = $date_service;
$this->dateFormatStorage = $date_format_storage;
Alex Pott
committed
}
/**
* {@inheritdoc}
*/
Alex Pott
committed
public static function create(ContainerInterface $container) {
Alex Pott
committed
return new static(
$container->get('date'),
catch
committed
$container->get('entity.manager')->getStorage('date_format')
Alex Pott
committed
);
}
Alex Pott
committed
Alex Pott
committed
/**
* Checks for an existing date format.
*
* @param string|int $entity_id
* The entity ID.
* @param array $element
* The form element.
* @param array $form_state
* The form state.
*
* @return bool
* TRUE if this format already exists, FALSE otherwise.
*/
public function exists($entity_id, array $element, array $form_state) {
return (bool) $this->dateFormatStorage
->getQuery()
Alex Pott
committed
->condition('id', $element['#field_prefix'] . $entity_id)
->execute();
}
/**
* Returns the date for a given format string.
*
* @param array $form
* An associative array containing the structure of the form.
* @param array $form_state
* An associative array containing the current state of the form.
*
* @return \Drupal\Core\Ajax\AjaxResponse
* An AJAX Response to update the date-time value of the date format.
*/
public static function dateTimeLookup(array $form, array $form_state) {
$format = '';
if (!empty($form_state['values']['date_format_pattern'])) {
$format = t('Displayed as %date_format', array('%date_format' => \Drupal::service('date')->format(REQUEST_TIME, 'custom', $form_state['values']['date_format_pattern'])));
Alex Pott
committed
}
Alex Pott
committed
// Return a command instead of a string, since the Ajax framework
// automatically prepends an additional empty DIV element for a string, which
// breaks the layout.
$response = new AjaxResponse();
$response->addCommand(new ReplaceCommand('#edit-date-format-suffix', '<small id="edit-date-format-suffix">' . $format . '</small>'));
return $response;
Alex Pott
committed
}
/**
* {@inheritdoc}
*/
Alex Pott
committed
public function form(array $form, array &$form_state) {
$form['label'] = array(
'#type' => 'textfield',
'#title' => 'Name',
'#maxlength' => 100,
'#description' => t('Name of the date format'),
'#default_value' => $this->entity->label(),
);
$form['id'] = array(
'#type' => 'machine_name',
'#description' => t('A unique machine-readable name. Can only contain lowercase letters, numbers, and underscores.'),
'#disabled' => !$this->entity->isNew(),
'#default_value' => $this->entity->id(),
'#machine_name' => array(
'exists' => array($this, 'exists'),
Angie Byron
committed
'replace_pattern' =>'([^a-z0-9_]+)|(^custom$)',
'error' => 'The machine-readable name must be unique, and can only contain lowercase letters, numbers, and underscores. Additionally, it can not be the reserved word "custom".',
Alex Pott
committed
),
);
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'));
Alex Pott
committed
}
else {
Alex Pott
committed
$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' => array($this, 'dateTimeLookup'),
'event' => 'keyup',
'progress' => array('type' => 'throbber', 'message' => NULL),
),
'#required' => TRUE,
);
Angie Byron
committed
$form['langcode'] = array(
'#type' => 'language_select',
'#title' => t('Language'),
'#languages' => Language::STATE_ALL,
'#default_value' => $this->entity->langcode,
);
Alex Pott
committed
Alex Pott
committed
return parent::form($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function validate(array $form, array &$form_state) {
parent::validate($form, $form_state);
// 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.
$pattern = trim($form_state['values']['date_format_pattern']);
foreach ($this->dateFormatStorage->loadMultiple() as $format) {
Alex Pott
committed
if ($format->getPattern() == $pattern && ($this->entity->isNew() || $format->id() != $this->entity->id())) {
$this->setFormError('date_format_pattern', $form_state, $this->t('This format already exists. Enter a unique format string.'));
continue;
}
Alex Pott
committed
}
}
/**
* {@inheritdoc}
*/
public function submit(array $form, array &$form_state) {
Angie Byron
committed
$form_state['redirect_route']['route_name'] = 'system.date_format_list';
Alex Pott
committed
$form_state['values']['pattern'][$this->patternType] = trim($form_state['values']['date_format_pattern']);
parent::submit($form, $form_state);
$this->entity->save();