diff --git a/core/modules/datetime/datetime.views.inc b/core/modules/datetime/datetime.views.inc index c3b7503..8b26bd5 100644 --- a/core/modules/datetime/datetime.views.inc +++ b/core/modules/datetime/datetime.views.inc @@ -11,6 +11,8 @@ * Implements hook_field_views_data(). */ function datetime_field_views_data(FieldStorageConfigInterface $field_storage) { + // @todo This code only covers configurable fields, handle base table fields + // in https://www.drupal.org/node/2489476. $data = views_field_default_views_data($field_storage); foreach ($data as $table_name => $table_data) { // Set the 'datetime' filter type. diff --git a/core/modules/datetime/src/Plugin/views/argument/Date.php b/core/modules/datetime/src/Plugin/views/argument/Date.php index d6b933b..7bd5cdf 100644 --- a/core/modules/datetime/src/Plugin/views/argument/Date.php +++ b/core/modules/datetime/src/Plugin/views/argument/Date.php @@ -4,6 +4,7 @@ * @file * Contains \Drupal\datetime\Plugin\views\Argument\Date. */ + namespace Drupal\datetime\Plugin\views\Argument; use Drupal\views\Plugin\views\argument\Date as NumericDate; @@ -27,7 +28,7 @@ class Date extends NumericDate { /** - * Override to account for dates stored as strings. + * {@inheritdoc} */ public function getDateField() { // Return the real field, since it is already in string format. @@ -35,8 +36,6 @@ public function getDateField() { } /** - * Override to account for dates stored as strings. - * * {@inheritdoc} */ public function getDateFormat($format) { diff --git a/core/modules/datetime/src/Plugin/views/filter/Date.php b/core/modules/datetime/src/Plugin/views/filter/Date.php index 136f8ff..5ca48e8 100644 --- a/core/modules/datetime/src/Plugin/views/filter/Date.php +++ b/core/modules/datetime/src/Plugin/views/filter/Date.php @@ -7,8 +7,11 @@ namespace Drupal\datetime\Plugin\views\filter; +use Drupal\Core\Datetime\DateFormatter; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\views\Plugin\views\filter\Date as NumericDate; +use Symfony\Component\DependencyInjection\ContainerInterface; /** * Date/time views filter. @@ -20,7 +23,43 @@ * * @ViewsFilter("datetime") */ -class Date extends NumericDate { +class Date extends NumericDate implements ContainerFactoryPluginInterface { + + /** + * The date formatter service. + * + * @var \Drupal\Core\Datetime\DateFormatter + */ + protected $dateFormatter; + + /** + * Constructs a new Date handler. + * + * @param array $configuration + * A configuration array containing information about the plugin instance. + * @param string $plugin_id + * The plugin ID for the plugin instance. + * @param mixed $plugin_definition + * The plugin implementation definition. + * @param \Drupal\Core\Datetime\DateFormatter $date_formatter + * The date formatter service. + */ + public function __construct(array $configuration, $plugin_id, $plugin_definition, DateFormatter $date_formatter) { + parent::__construct($configuration, $plugin_id, $plugin_definition); + $this->dateFormatter = $date_formatter; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $container->get('date.formatter') + ); + } /** * Date format for SQL conversion. @@ -40,8 +79,8 @@ protected function opBetween($field) { $b = intval(strtotime($this->value['max'], $origin)); // Convert to ISO format and format for query. - $a = $this->query->getDateFormat("'" . format_date($a, 'custom', 'c') . "'", static::$dateFormat, TRUE); - $b = $this->query->getDateFormat("'" . format_date($b, 'custom', 'c') . "'", static::$dateFormat, TRUE); + $a = $this->query->getDateFormat("'" . $this->dateFormatter->format($a, 'custom', 'c') . "'", static::$dateFormat, TRUE); + $b = $this->query->getDateFormat("'" . $this->dateFormatter->format($b, 'custom', 'c') . "'", static::$dateFormat, TRUE); // This is safe because we are manually scrubbing the values. $operator = strtoupper($this->operator); @@ -57,7 +96,7 @@ protected function opSimple($field) { $value = intval(strtotime($this->value['value'], $origin)); // Convert to ISO. - $value = $this->query->getDateFormat("'" . format_date($value, 'custom', 'c') . "'", static::$dateFormat, TRUE); + $value = $this->query->getDateFormat("'" . $this->dateFormatter->format($value, 'custom', 'c') . "'", static::$dateFormat, TRUE); // This is safe because we are manually scrubbing the value. $field = $this->query->getDateFormat($field, static::$dateFormat, TRUE); diff --git a/core/modules/datetime/src/Tests/Views/FilterDateTimeTest.php b/core/modules/datetime/src/Tests/Views/FilterDateTimeTest.php index 051e24d..be36791 100644 --- a/core/modules/datetime/src/Tests/Views/FilterDateTimeTest.php +++ b/core/modules/datetime/src/Tests/Views/FilterDateTimeTest.php @@ -32,7 +32,7 @@ public function setUp() { '2000-10-10', '2001-10-10', '2002-10-10', - format_date(REQUEST_TIME + 86400, 'custom', 'Y-m-d'), + \Drupal::service('date.formatter')->format(REQUEST_TIME + 86400, 'custom', 'Y-m-d'), ); foreach ($dates as $date) { $this->nodes[] = $this->drupalCreateNode(array(