diff --git a/core/lib/Drupal/Core/Datetime/Date.php b/core/lib/Drupal/Core/Datetime/Date.php index 1ff241e..5567331 100644 --- a/core/lib/Drupal/Core/Datetime/Date.php +++ b/core/lib/Drupal/Core/Datetime/Date.php @@ -40,7 +40,7 @@ class Date { protected $languageManager; /** - * Constructs a DateFormats object. + * Constructs a Date object. * * @param \Drupal\Core\Entity\EntityManager $entity_manager * The entity manager. @@ -109,7 +109,7 @@ public function format($timestamp, $type = 'medium', $format = '', $timezone = N // Fall back to medium if a format was not found. if (empty($format)) { - $format = $this->dateFormatStorage->load('medium')->getPattern($key); + $format = $this->dateFormatStorage->load('fallback')->getPattern($key); } // Call $date->format(). diff --git a/core/modules/datetime/lib/Drupal/datetime/Plugin/field/formatter/DatetimeDefaultFormatter.php b/core/modules/datetime/lib/Drupal/datetime/Plugin/field/formatter/DatetimeDefaultFormatter.php index 5364cfd..2ed1b8a 100644 --- a/core/modules/datetime/lib/Drupal/datetime/Plugin/field/formatter/DatetimeDefaultFormatter.php +++ b/core/modules/datetime/lib/Drupal/datetime/Plugin/field/formatter/DatetimeDefaultFormatter.php @@ -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 $dateService + * 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 $dateService, EntityStorageControllerInterface $date_storage) { + parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode); + + $this->dateService = $dateService; + $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,17 @@ public function viewElements(EntityInterface $entity, $langcode, array $items) { */ function dateFormat($date) { $format_type = $this->getSetting('format_type'); - return \Drupal::service('date')->format($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 = \Drupal::entityManager() - ->getStorageController('date_format') - ->loadMultiple(); + $format_types = $this->dateStorage->loadMultiple(); foreach ($format_types as $type => $type_info) { - $format = \Drupal::service('date')->format($time->format('U'), $type); + $format = $this->dateService->format($time->format('U'), $type); $options[$type] = $type_info->label() . ' (' . $format . ')'; } diff --git a/core/modules/datetime/lib/Drupal/datetime/Plugin/field/widget/DatetimeDefaultWidget.php b/core/modules/datetime/lib/Drupal/datetime/Plugin/field/widget/DatetimeDefaultWidget.php index 6e243f4..5fd0a1e 100644 --- a/core/modules/datetime/lib/Drupal/datetime/Plugin/field/widget/DatetimeDefaultWidget.php +++ b/core/modules/datetime/lib/Drupal/datetime/Plugin/field/widget/DatetimeDefaultWidget.php @@ -31,6 +31,13 @@ class DateTimeDefaultWidget extends WidgetBase { /** + * The date format storage. + * + * @var \Drupal\Core\Entity\EntityStorageControllerInterface + */ + protected $dateStorage; + + /** * {@inheritdoc} */ public function __construct($plugin_id, array $plugin_definition, FieldDefinitionInterface $field_definition, array $settings) { @@ -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 = entity_load('date_format', 'html_date')->getPattern($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 = entity_load('date_format', 'html_date')->getPattern($format_type); - $time_format = entity_load('date_format', 'html_time')->getPattern($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; diff --git a/core/modules/system/config/system.date_format.fallback.yml b/core/modules/system/config/system.date_format.fallback.yml new file mode 100644 index 0000000..e8cab2f --- /dev/null +++ b/core/modules/system/config/system.date_format.fallback.yml @@ -0,0 +1,10 @@ +id: fallback +uuid: cf0ad071-ded7-4750-b008-8c74f200ff8d +label: 'Fallback date' +status: '1' +langcode: en +locked: '1' +locales: { } +pattern: + php: 'D, m/d/Y - H:i' + intl: 'ccc, MM/dd/yyyy - kk:mm' diff --git a/core/modules/system/lib/Drupal/system/DateFormatListController.php b/core/modules/system/lib/Drupal/system/DateFormatListController.php index c9eb361..e08c825 100644 --- a/core/modules/system/lib/Drupal/system/DateFormatListController.php +++ b/core/modules/system/lib/Drupal/system/DateFormatListController.php @@ -25,7 +25,7 @@ class DateFormatListController extends ConfigEntityListController { * * @var \Drupal\Core\Datetime\Date */ - protected $date; + protected $dateService; /** * Constructs a new DateFormatListController object. @@ -38,13 +38,13 @@ class DateFormatListController extends ConfigEntityListController { * The entity storage controller class. * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler * The module handler to invoke hooks on. - * @param \Drupal\Core\Datetime\Date $date + * @param \Drupal\Core\Datetime\Date $date_service * The date service. */ - public function __construct($entity_type, array $entity_info, EntityStorageControllerInterface $storage, ModuleHandlerInterface $module_handler, Date $date) { + public function __construct($entity_type, array $entity_info, EntityStorageControllerInterface $storage, ModuleHandlerInterface $module_handler, Date $date_service) { parent::__construct($entity_type, $entity_info, $storage, $module_handler); - $this->date = $date; + $this->dateService = $date_service; } /** @@ -86,7 +86,7 @@ public function buildHeader() { public function buildRow(EntityInterface $entity) { $row['id'] = $entity->id(); $row['label'] = String::checkPlain($entity->label()); - $row['pattern'] = $this->date->format(REQUEST_TIME, $entity->id()); + $row['pattern'] = $this->dateService->format(REQUEST_TIME, $entity->id()); $row['operations']['data'] = $this->buildOperations($entity); return $row; } diff --git a/core/modules/system/lib/Drupal/system/Form/DateFormatDeleteForm.php b/core/modules/system/lib/Drupal/system/Form/DateFormatDeleteForm.php index 1d8f2d5..3d3dfc7 100644 --- a/core/modules/system/lib/Drupal/system/Form/DateFormatDeleteForm.php +++ b/core/modules/system/lib/Drupal/system/Form/DateFormatDeleteForm.php @@ -24,20 +24,20 @@ class DateFormatDeleteForm extends EntityConfirmFormBase implements EntityContro * * @var \Drupal\Core\Datetime\Date */ - protected $date; + protected $dateService; /** * Constructs an DateFormatDeleteForm object. * * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler * The module handler service. - * @param \Drupal\Core\Datetime\Date $date + * @param \Drupal\Core\Datetime\Date $date_service * The date service. */ - public function __construct(ModuleHandlerInterface $module_handler, Date $date) { + public function __construct(ModuleHandlerInterface $module_handler, Date $date_service) { parent::__construct($module_handler); - $this->date = $date; + $this->dateService = $date_service; } /** @@ -56,7 +56,7 @@ public static function createInstance(ContainerInterface $container, $entity_typ public function getQuestion() { return t('Are you sure you want to remove the format %name : %format?', array( '%name' => $this->entity->label(), - '%format' => $this->date->format(REQUEST_TIME, $this->entity->id())) + '%format' => $this->dateService->format(REQUEST_TIME, $this->entity->id())) ); } diff --git a/core/modules/system/lib/Drupal/system/Form/DateFormatEditForm.php b/core/modules/system/lib/Drupal/system/Form/DateFormatEditForm.php index 0343f23..ebf4df8 100644 --- a/core/modules/system/lib/Drupal/system/Form/DateFormatEditForm.php +++ b/core/modules/system/lib/Drupal/system/Form/DateFormatEditForm.php @@ -18,7 +18,7 @@ class DateFormatEditForm extends DateFormatFormBase { public function form(array $form, array &$form_state) { $form = parent::form($form, $form_state); - $now = t('Displayed as %date', array('%date' => $this->date->format(REQUEST_TIME, $this->entity->id()))); + $now = t('Displayed as %date', array('%date' => $this->dateService->format(REQUEST_TIME, $this->entity->id()))); $form['date_format_pattern']['#field_suffix'] = ' ' . $now . ''; $form['date_format_pattern']['#default_value'] = $this->entity->getPattern($this->patternType); diff --git a/core/modules/system/lib/Drupal/system/Form/DateFormatFormBase.php b/core/modules/system/lib/Drupal/system/Form/DateFormatFormBase.php index e453a8e..4d347c5 100644 --- a/core/modules/system/lib/Drupal/system/Form/DateFormatFormBase.php +++ b/core/modules/system/lib/Drupal/system/Form/DateFormatFormBase.php @@ -41,7 +41,7 @@ * * @var \Drupal\Core\Datetime\Date */ - protected $date; + protected $dateService; /** * Constructs a new date format form. @@ -50,17 +50,17 @@ * The module handler. * @param \Drupal\Core\Entity\Query\QueryFactory $query_factory * The entity query factory. - * @param \Drupal\Core\Datetime\Date $date + * @param \Drupal\Core\Datetime\Date $date_service * The date service. */ - function __construct(ModuleHandlerInterface $module_handler, QueryFactory $query_factory, Date $date) { + function __construct(ModuleHandlerInterface $module_handler, QueryFactory $query_factory, Date $date_service) { parent::__construct($module_handler); $date = new DrupalDateTime(); $this->patternType = $date->canUseIntl() ? DrupalDateTime::INTL : DrupalDateTime::PHP; $this->queryFactory = $query_factory; - $this->date = $date; + $this->dateService = $date_service; } /**