diff --git a/core/modules/datetime/src/Plugin/views/filter/Date.php b/core/modules/datetime/src/Plugin/views/filter/Date.php index a2f0d3a..0a51ef2 100644 --- a/core/modules/datetime/src/Plugin/views/filter/Date.php +++ b/core/modules/datetime/src/Plugin/views/filter/Date.php @@ -105,15 +105,15 @@ protected function opBetween($field) { ? DATETIME_STORAGE_TIMEZONE : drupal_get_user_timezone(); - // Although both 'min' and 'max' values are required, - // default empty 'min' value as UNIX timestamp 0. + // Although both 'min' and 'max' values are required, default empty 'min' + // value as UNIX timestamp 0. $min = (!empty($this->value['min'])) ? $this->value['min'] : '@0'; $a = new DateTimePlus($min, new \DateTimeZone($timezone)); $b = new DateTimePlus($this->value['max'], new \DateTimeZone($timezone)); - // For 'date' types with offset, user's 'now' could not be UTC 'now'. - // So calculate the difference between user's timezone and UTC. + // For 'date' types with offset, user's 'now' could not be UTC 'now'. So + // calculate the difference between user's timezone and UTC. $origin_offset = 0; if ($this->dateFormat === DATETIME_DATE_STORAGE_FORMAT && $this->value['type'] == 'offset') { $origin_offset = $origin_offset + timezone_offset_get(new \DateTimeZone(drupal_get_user_timezone()), new \DateTime($this->value['min'], new \DateTimeZone($timezone))); @@ -140,15 +140,14 @@ protected function opSimple($field) { $value = new DateTimePlus($this->value['value'], new \DateTimeZone($timezone)); - // For 'date' types with offset, user's 'now' could not be UTC 'now'. - // So calculate the difference between user's timezone and UTC. + // For 'date' types with offset, user's 'now' could not be UTC 'now'. So + // calculate the difference between user's timezone and UTC. $origin_offset = 0; if ($this->dateFormat === DATETIME_DATE_STORAGE_FORMAT && $this->value['type'] == 'offset') { $origin_offset = $origin_offset + timezone_offset_get(new \DateTimeZone(drupal_get_user_timezone()), new \DateTime($this->value['value'], new \DateTimeZone($timezone))); } - // Convert to ISO. UTC timezone is used since - // dates are stored in UTC. + // Convert to ISO. UTC timezone is used since dates are stored in UTC. $value = $this->query->getDateFormat($this->query->getDateField("'" . $this->dateFormatter->format($value->getTimestamp() + $origin_offset, 'custom', DATETIME_DATETIME_STORAGE_FORMAT, DATETIME_STORAGE_TIMEZONE) . "'", TRUE, $this->calculateOffset), $this->dateFormat, TRUE); // This is safe because we are manually scrubbing the value. diff --git a/core/modules/datetime/tests/src/Kernel/Views/ArgumentDateTimeTest.php b/core/modules/datetime/tests/src/Kernel/Views/ArgumentDateTimeTest.php index 6d75e36..c20e3cf 100644 --- a/core/modules/datetime/tests/src/Kernel/Views/ArgumentDateTimeTest.php +++ b/core/modules/datetime/tests/src/Kernel/Views/ArgumentDateTimeTest.php @@ -74,7 +74,7 @@ public function testDatetimeArgumentYear() { $this->assertIdenticalResultset($view, $expected, $this->map); $view->destroy(); - // Test s different system timezone with the same nodes. + // Tests different system timezone with the same nodes. $this->setSiteTimezone('America/Vancouver'); $view->setDisplay('default'); diff --git a/core/modules/datetime/tests/src/Kernel/Views/FilterDateTest.php b/core/modules/datetime/tests/src/Kernel/Views/FilterDateTest.php index b59a4da..f4a6342 100644 --- a/core/modules/datetime/tests/src/Kernel/Views/FilterDateTest.php +++ b/core/modules/datetime/tests/src/Kernel/Views/FilterDateTest.php @@ -55,8 +55,7 @@ protected function setUp($import_test_views = TRUE) { $storage->setSetting('datetime_type', DateTimeItem::DATETIME_TYPE_DATE); $storage->save(); - // Retrieve tomorrow, today and yesterday dates - // just to create the nodes. + // Retrieve tomorrow, today and yesterday dates just to create the nodes. $timestamp = $this->getUTCEquivalentOfUserNowAsTimestamp(); $dates = $this->getRelativeDateValuesFromTimestamp($timestamp); @@ -92,8 +91,8 @@ public function testDateOffsets() { // Test simple operations. $view->initHandlers(); - // A greater than or equal to 'now', should return the 'today' and - // the 'tomorrow' node. + // A greater than or equal to 'now', should return the 'today' and the + // 'tomorrow' node. $view->filter[$field]->operator = '>='; $view->filter[$field]->value['type'] = 'offset'; $view->filter[$field]->value['value'] = 'now'; @@ -152,8 +151,8 @@ public function testDateIs() { // Test simple operations. $view->initHandlers(); - // Filtering with nodes date-only values (format: Y-m-d) to test - // UTC conversion does NOT change the day. + // Filtering with nodes date-only values (format: Y-m-d) to test UTC + // conversion does NOT change the day. $view->filter[$field]->operator = '='; $view->filter[$field]->value['type'] = 'date'; $view->filter[$field]->value['value'] = $this->nodes[2]->field_date->first()->getValue()['value']; @@ -165,8 +164,8 @@ public function testDateIs() { $this->assertIdenticalResultset($view, $expected_result, $this->map); $view->destroy(); - // Test offset for between operator. - // Only 'today' and 'tomorrow' nodes should appear. + // Test offset for between operator. Only 'today' and 'tomorrow' nodes + // should appear. $view->initHandlers(); $view->filter[$field]->operator = 'between'; $view->filter[$field]->value['type'] = 'date'; @@ -186,10 +185,9 @@ public function testDateIs() { /** * Returns UTC timestamp of user's TZ 'now'. * - * The date field stores date_only values without conversion, - * considering them already as UTC. - * This method returns the UTC equivalent of user's "now" - * as a unix timestamp, so they match using Y-m-d format. + * The date field stores date_only values without conversion, considering them + * already as UTC. This method returns the UTC equivalent of user's 'now' as a + * unix timestamp, so they match using Y-m-d format. * * @return int * Unix timestamp. @@ -208,8 +206,8 @@ protected function getUTCEquivalentOfUserNowAsTimestamp() { * Unix Timestamp equivalent to user's "now". * * @return array - * An array of DATETIME_DATE_STORAGE_FORMAT date values. - * In order tomorrow, today and yesterday. + * An array of DATETIME_DATE_STORAGE_FORMAT date values. In order tomorrow, + * today and yesterday. */ protected function getRelativeDateValuesFromTimestamp($timestamp) { return [ diff --git a/core/modules/views/src/Plugin/views/query/DateSqlBase.php b/core/modules/views/src/Plugin/views/query/DateSqlBase.php deleted file mode 100644 index 3f5bfa6..0000000 --- a/core/modules/views/src/Plugin/views/query/DateSqlBase.php +++ /dev/null @@ -1,29 +0,0 @@ -database = $database; - } - -} diff --git a/core/modules/views/src/Plugin/views/query/MysqlDateSql.php b/core/modules/views/src/Plugin/views/query/MysqlDateSql.php index c32088a..432adfb 100644 --- a/core/modules/views/src/Plugin/views/query/MysqlDateSql.php +++ b/core/modules/views/src/Plugin/views/query/MysqlDateSql.php @@ -2,10 +2,19 @@ namespace Drupal\views\Plugin\views\query; +use Drupal\Core\Database\Connection; + /** * MySQL-specific date handling. */ -class MysqlDateSql extends DateSqlBase { +class MysqlDateSql implements DateSqlInterface { + + /** + * The database connection. + * + * @var \Drupal\Core\Database\Connection + */ + protected $database; /** * An array of PHP-to-MySQL replacement patterns. @@ -30,6 +39,16 @@ class MysqlDateSql extends DateSqlBase { ]; /** + * Constructs the MySQL-specific date sql class. + * + * @param \Drupal\Core\Database\Connection $database + * The database connection. + */ + public function __construct(Connection $database) { + $this->database = $database; + } + + /** * {@inheritdoc} */ public function getDateField($field, $string_date) { diff --git a/core/modules/views/src/Plugin/views/query/PostgresqlDateSql.php b/core/modules/views/src/Plugin/views/query/PostgresqlDateSql.php index f54fb27..1960b59 100644 --- a/core/modules/views/src/Plugin/views/query/PostgresqlDateSql.php +++ b/core/modules/views/src/Plugin/views/query/PostgresqlDateSql.php @@ -2,10 +2,19 @@ namespace Drupal\views\Plugin\views\query; +use Drupal\Core\Database\Connection; + /** * PostgreSQL-specific date handling. */ -class PostgresqlDateSql extends DateSqlBase { +class PostgresqlDateSql implements DateSqlInterface { + + /** + * The database connection. + * + * @var \Drupal\Core\Database\Connection + */ + protected $database; /** * An array of PHP-to-PostgreSQL replacement patterns. @@ -17,8 +26,7 @@ class PostgresqlDateSql extends DateSqlBase { 'y' => 'YY', 'M' => 'Mon', 'm' => 'MM', - // No format for Numeric representation of a month, without leading - // zeros. + // No format for Numeric representation of a month, without leading zeros. 'n' => 'MM', 'F' => 'Month', 'D' => 'Dy', @@ -35,6 +43,16 @@ class PostgresqlDateSql extends DateSqlBase { ]; /** + * Constructs the PostgreSQL-specific date sql class. + * + * @param \Drupal\Core\Database\Connection $database + * The database connection. + */ + public function __construct(Connection $database) { + $this->database = $database; + } + + /** * {@inheritdoc} */ public function getDateField($field, $string_date) { diff --git a/core/modules/views/src/Plugin/views/query/SqliteDateSql.php b/core/modules/views/src/Plugin/views/query/SqliteDateSql.php index 9f34452..939d46c 100644 --- a/core/modules/views/src/Plugin/views/query/SqliteDateSql.php +++ b/core/modules/views/src/Plugin/views/query/SqliteDateSql.php @@ -2,10 +2,19 @@ namespace Drupal\views\Plugin\views\query; +use Drupal\Core\Database\Connection; + /** * SQLite-specific date handling. */ -class SqliteDateSql extends DateSqlBase { +class SqliteDateSql implements DateSqlInterface { + + /** + * The database connection. + * + * @var \Drupal\Core\Database\Connection + */ + protected $database; /** * An array of PHP-to-SQLite date replacement patterns. @@ -41,6 +50,16 @@ class SqliteDateSql extends DateSqlBase { ]; /** + * Constructs the SQLite-specific date sql class. + * + * @param \Drupal\Core\Database\Connection $database + * The database connection. + */ + public function __construct(Connection $database) { + $this->database = $database; + } + + /** * {@inheritdoc} */ public function getDateField($field, $string_date) { @@ -56,8 +75,8 @@ public function getDateField($field, $string_date) { public function getDateFormat($field, $format) { $format = strtr($format, static::$replace); - // SQLite does not have a ISO week substitution string, so it needs - // special handling. + // SQLite does not have a ISO week substitution string, so it needs special + // handling. // @see http://wikipedia.org/wiki/ISO_week_date#Calculation // @see http://stackoverflow.com/a/15511864/1499564 if ($format === '%W') { @@ -66,10 +85,10 @@ public function getDateFormat($field, $format) { else { $expression = "strftime('$format', $field, 'unixepoch')"; } - // The expression yields a string, but the comparison value is an - // integer in case the comparison value is a float, integer, or numeric. - // All of the above SQLite format tokens only produce integers. However, - // the given $format may contain 'Y-m-d', which results in a string. + // The expression yields a string, but the comparison value is an integer in + // case the comparison value is a float, integer, or numeric. All of the + // above SQLite format tokens only produce integers. However, the given + // $format may contain 'Y-m-d', which results in a string. // @see \Drupal\Core\Database\Driver\sqlite\Connection::expandArguments() // @see http://www.sqlite.org/lang_datefunc.html // @see http://www.sqlite.org/lang_expr.html#castexpr diff --git a/core/modules/views/views.services.yml b/core/modules/views/views.services.yml index e0fc18b..ffed394 100644 --- a/core/modules/views/views.services.yml +++ b/core/modules/views/views.services.yml @@ -81,15 +81,15 @@ services: tags: - { name: 'event_subscriber' } views.date_sql: - class: Drupal\datetime\GenericDateField - tags: - - { name: backend_overridable } - mysql.views.date_sql: class: Drupal\views\Plugin\views\query\MysqlDateSql arguments: ['@database'] + tags: + - { name: backend_overridable } pgsql.views.date_sql: class: Drupal\views\Plugin\views\query\PostgresqlDateSql arguments: ['@database'] + public: false sqlite.views.date_sql: class: Drupal\views\Plugin\views\query\SqliteDateSql arguments: ['@database'] + public: false