diff --git a/core/core.services.yml b/core/core.services.yml index d915e10..326cae3 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -488,7 +488,7 @@ services: arguments: ['@module_handler'] date: class: Drupal\Core\Datetime\Date - arguments: ['@config.factory', '@language_manager'] + arguments: ['@plugin.manager.entity', '@language_manager'] feed.bridge.reader: class: Drupal\Component\Bridge\ZfExtensionManagerSfContainer calls: diff --git a/core/lib/Drupal/Core/Datetime/Date.php b/core/lib/Drupal/Core/Datetime/Date.php index 087fa3b..78709e1 100644 --- a/core/lib/Drupal/Core/Datetime/Date.php +++ b/core/lib/Drupal/Core/Datetime/Date.php @@ -8,8 +8,8 @@ namespace Drupal\Core\Datetime; use Drupal\Component\Utility\Xss; -use Drupal\Core\Config\ConfigFactory; use Drupal\Core\Datetime\DrupalDateTime; +use Drupal\Core\Entity\EntityManager; use Drupal\Core\Language\Language; use Drupal\Core\Language\LanguageManager; @@ -26,11 +26,11 @@ class Date { protected $timezones; /** - * The config factory. + * The date format storage. * - * @var \Drupal\Core\Config\ConfigFactory + * @var \Drupal\Core\Entity\EntityStorageControllerInterface */ - protected $configFactory; + protected $dateStorage; /** * Language manager for retrieving the default langcode when none is specified. @@ -42,13 +42,13 @@ class Date { /** * Constructs a DateFormats object. * - * @param \Drupal\Core\Config\ConfigFactory $config_factory - * The config factory. + * @param \Drupal\Core\Entity\EntityManager $entity_manager + * The entity manager. * @param \Drupal\Core\Language\LanguageManager $language_manager * The language manager. */ - public function __construct(ConfigFactory $config_factory, LanguageManager $language_manager) { - $this->configFactory = $config_factory; + public function __construct(EntityManager $entity_manager, LanguageManager $language_manager) { + $this->dateStorage = $entity_manager->getStorageController('date_format'); $this->languageManager = $language_manager; } @@ -103,14 +103,13 @@ public function format($timestamp, $type = 'medium', $format = '', $timezone = N $key = $date->canUseIntl() ? DrupalDateTime::INTL : DrupalDateTime::PHP; // If we have a non-custom date format use the provided date format pattern. - $config = $this->configFactory->get('system.date'); - if ($date_format = entity_load('date_format', $type)) { + if ($date_format = $this->dateStorage->load($type)) { $format = $date_format->getPattern($key); } // Fall back to medium if a format was not found. if (empty($format)) { - $format = entity_load('date_format', 'medium')->getPattern($key); + $format = $this->dateStorage->load('medium')->getPattern($key); } // Call $date->format(). diff --git a/core/modules/system/lib/Drupal/system/DateFormatListController.php b/core/modules/system/lib/Drupal/system/DateFormatListController.php index 0c76642..c9eb361 100644 --- a/core/modules/system/lib/Drupal/system/DateFormatListController.php +++ b/core/modules/system/lib/Drupal/system/DateFormatListController.php @@ -9,7 +9,11 @@ use Drupal\Component\Utility\String; use Drupal\Core\Config\Entity\ConfigEntityListController; +use Drupal\Core\Datetime\Date; use Drupal\Core\Entity\EntityInterface; +use Drupal\Core\Entity\EntityStorageControllerInterface; +use Drupal\Core\Extension\ModuleHandlerInterface; +use Symfony\Component\DependencyInjection\ContainerInterface; /** * Provides a listing of date formats. @@ -17,6 +21,46 @@ class DateFormatListController extends ConfigEntityListController { /** + * The date service. + * + * @var \Drupal\Core\Datetime\Date + */ + protected $date; + + /** + * Constructs a new DateFormatListController object. + * + * @param string $entity_type + * The type of entity to be listed. + * @param array $entity_info + * An array of entity info for the entity type. + * @param \Drupal\Core\Entity\EntityStorageControllerInterface $storage + * 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 + * The date service. + */ + public function __construct($entity_type, array $entity_info, EntityStorageControllerInterface $storage, ModuleHandlerInterface $module_handler, Date $date) { + parent::__construct($entity_type, $entity_info, $storage, $module_handler); + + $this->date = $date; + } + + /** + * {@inheritdoc} + */ + public static function createInstance(ContainerInterface $container, $entity_type, array $entity_info) { + return new static( + $entity_type, + $entity_info, + $container->get('plugin.manager.entity')->getStorageController($entity_type), + $container->get('module_handler'), + $container->get('date') + ); + } + + /** * {@inheritdoc} */ public function load() { @@ -42,7 +86,7 @@ public function buildHeader() { public function buildRow(EntityInterface $entity) { $row['id'] = $entity->id(); $row['label'] = String::checkPlain($entity->label()); - $row['pattern'] = format_date(REQUEST_TIME, $entity->id()); + $row['pattern'] = $this->date->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 8356faa..1d8f2d5 100644 --- a/core/modules/system/lib/Drupal/system/Form/DateFormatDeleteForm.php +++ b/core/modules/system/lib/Drupal/system/Form/DateFormatDeleteForm.php @@ -7,12 +7,48 @@ namespace Drupal\system\Form; +use Drupal\Core\Controller\ControllerInterface; +use Drupal\Core\Datetime\Date; use Drupal\Core\Entity\EntityConfirmFormBase; +use Drupal\Core\Entity\EntityControllerInterface; +use Drupal\Core\Extension\ModuleHandlerInterface; +use Symfony\Component\DependencyInjection\ContainerInterface; /** * Builds a form to delete a date format. */ -class DateFormatDeleteForm extends EntityConfirmFormBase { +class DateFormatDeleteForm extends EntityConfirmFormBase implements EntityControllerInterface { + + /** + * The date service. + * + * @var \Drupal\Core\Datetime\Date + */ + protected $date; + + /** + * Constructs an DateFormatDeleteForm object. + * + * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler + * The module handler service. + * @param \Drupal\Core\Datetime\Date $date + * The date service. + */ + public function __construct(ModuleHandlerInterface $module_handler, Date $date) { + parent::__construct($module_handler); + + $this->date = $date; + } + + /** + * {@inheritdoc} + */ + public static function createInstance(ContainerInterface $container, $entity_type, array $entity_info) { + return new static( + $container->get('module_handler'), + $container->get('date') + ); + } /** * {@inheritdoc} @@ -20,7 +56,7 @@ class DateFormatDeleteForm extends EntityConfirmFormBase { public function getQuestion() { return t('Are you sure you want to remove the format %name : %format?', array( '%name' => $this->entity->label(), - '%format' => format_date(REQUEST_TIME, $this->entity->id())) + '%format' => $this->date->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 a8a3c43..0343f23 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' => format_date(REQUEST_TIME, $this->entity->id()))); + $now = t('Displayed as %date', array('%date' => $this->date->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 64cc048..e453a8e 100644 --- a/core/modules/system/lib/Drupal/system/Form/DateFormatFormBase.php +++ b/core/modules/system/lib/Drupal/system/Form/DateFormatFormBase.php @@ -9,6 +9,7 @@ use Drupal\Core\Ajax\AjaxResponse; use Drupal\Core\Ajax\ReplaceCommand; +use Drupal\Core\Datetime\Date; use Drupal\Core\Entity\EntityControllerInterface; use Drupal\Core\Extension\ModuleHandlerInterface; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -36,20 +37,30 @@ protected $queryFactory; /** + * The date service. + * + * @var \Drupal\Core\Datetime\Date + */ + protected $date; + + /** * Constructs a new date format form. * * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler * The module handler. * @param \Drupal\Core\Entity\Query\QueryFactory $query_factory * The entity query factory. + * @param \Drupal\Core\Datetime\Date $date + * The date service. */ - function __construct(ModuleHandlerInterface $module_handler, QueryFactory $query_factory) { + function __construct(ModuleHandlerInterface $module_handler, QueryFactory $query_factory, Date $date) { parent::__construct($module_handler); $date = new DrupalDateTime(); $this->patternType = $date->canUseIntl() ? DrupalDateTime::INTL : DrupalDateTime::PHP; $this->queryFactory = $query_factory; + $this->date = $date; } /** @@ -58,7 +69,8 @@ function __construct(ModuleHandlerInterface $module_handler, QueryFactory $query public static function createInstance(ContainerInterface $container, $entity_type, array $entity_info) { return new static( $container->get('module_handler'), - $container->get('entity.query') + $container->get('entity.query'), + $container->get('date') ); } @@ -96,7 +108,7 @@ public function exists($entity_id, array $element, array $form_state) { 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' => format_date(REQUEST_TIME, 'custom', $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']))); } // Return a command instead of a string, since the Ajax framework // automatically prepends an additional empty DIV element for a string, which diff --git a/core/modules/system/system.admin.inc b/core/modules/system/system.admin.inc index 0413128..e103c79 100644 --- a/core/modules/system/system.admin.inc +++ b/core/modules/system/system.admin.inc @@ -1374,6 +1374,7 @@ function system_date_format_localize_form($form, &$form_state, $langcode) { ); // Get list of available formats. + $date_service = Drupal::service('date'); $formats = Drupal::entityManager() ->getStorageController('date_format') ->loadMultiple(); @@ -1381,7 +1382,7 @@ function system_date_format_localize_form($form, &$form_state, $langcode) { foreach ($formats as $date_format_id => $format_info) { // Ignore values that are localized. if (!$format_info->hasLocales()) { - $choices[$date_format_id] = format_date(REQUEST_TIME, $date_format_id); + $choices[$date_format_id] = $date_service->format(REQUEST_TIME, $date_format_id); } } @@ -1389,7 +1390,7 @@ function system_date_format_localize_form($form, &$form_state, $langcode) { $config_prefix = 'locale.config.' . $langcode . '.system.date_format'; foreach (config_get_storage_names_with_prefix($config_prefix) as $config_id) { $date_format_id = substr($config_id, strlen($config_prefix. '.')); - $choices[$date_format_id] = format_date(REQUEST_TIME, $date_format_id); + $choices[$date_format_id] = $date_service->format(REQUEST_TIME, $date_format_id); } // Display a form field for each format type. diff --git a/core/modules/views/lib/Drupal/views/Plugin/views/field/Date.php b/core/modules/views/lib/Drupal/views/Plugin/views/field/Date.php index ff2203a..58fd587 100644 --- a/core/modules/views/lib/Drupal/views/Plugin/views/field/Date.php +++ b/core/modules/views/lib/Drupal/views/Plugin/views/field/Date.php @@ -8,6 +8,9 @@ namespace Drupal\views\Plugin\views\field; use Drupal\Component\Annotation\PluginID; +use Drupal\Core\Entity\EntityStorageControllerInterface; +use Symfony\Component\DependencyInjection\ContainerInterface; +use Drupal\Core\Datetime\Date as DateService; /** * A handler to provide proper displays for dates. @@ -18,6 +21,54 @@ */ class Date extends FieldPluginBase { + /** + * The date service. + * + * @var \Drupal\Core\Datetime\Date + */ + protected $date; + + /** + * The date format storage. + * + * @var \Drupal\Core\Entity\EntityStorageControllerInterface + */ + protected $dateStorage; + + /** + * Constructs a new Date object. + * + * @param array $configuration + * A configuration array containing information about the plugin instance. + * @param string $plugin_id + * The plugin ID for the plugin instance. + * @param array $plugin_definition + * The plugin implementation definition. + * @param \Drupal\Core\Datetime\Date $date + * The date service. + * @param \Drupal\Core\Entity\EntityStorageControllerInterface $date_storage + * The date format storage. + */ + public function __construct(array $configuration, $plugin_id, array $plugin_definition, DateService $date, EntityStorageControllerInterface $date_storage) { + parent::__construct($configuration, $plugin_id, $plugin_definition); + + $this->date = $date; + $this->dateStorage = $date_storage; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, array $plugin_definition) { + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $container->get('date'), + $container->get('plugin.manager.entity')->getStorageController('date_format') + ); + } + protected function defineOptions() { $options = parent::defineOptions(); @@ -31,11 +82,8 @@ protected function defineOptions() { public function buildOptionsForm(&$form, &$form_state) { $date_formats = array(); - $date_types = \Drupal::entityManager() - ->getStorageController('date_format') - ->loadMultiple(); - foreach ($date_types as $machine_name => $value) { - $date_formats[$machine_name] = t('@name format: @date', array('@name' => $value->label(), '@date' => format_date(REQUEST_TIME, $machine_name))); + foreach ($this->dateStorage->loadMultiple() as $machine_name => $value) { + $date_formats[$machine_name] = t('@name format: @date', array('@name' => $value->label(), '@date' => $this->date->format(REQUEST_TIME, $machine_name))); } $form['date_format'] = array(