From e6e4758262aa9d6afa38185ac33a04c2026090b6 Mon Sep 17 00:00:00 2001
From: Alex Pott <alex.a.pott@googlemail.com>
Date: Mon, 25 Jan 2016 16:04:26 +0000
Subject: [PATCH] Issue #2567815 by clemens.tolboom, Lendude, geertvd,
 mpdonadio, xjm, alexpott: Add week, date, and year-month Views argument
 plugins

---
 core/modules/datetime/datetime.views.inc      |  7 +-
 .../src/Plugin/views/argument/FullDate.php    | 22 ++++++
 .../src/Plugin/views/argument/WeekDate.php    | 22 ++++++
 .../Plugin/views/argument/YearMonthDate.php   | 22 ++++++
 .../src/Tests/Views/ArgumentDateTimeTest.php  | 67 +++++++++++++++++++
 .../views.view.test_argument_datetime.yml     | 42 ++++++++++++
 6 files changed, 180 insertions(+), 2 deletions(-)
 create mode 100644 core/modules/datetime/src/Plugin/views/argument/FullDate.php
 create mode 100644 core/modules/datetime/src/Plugin/views/argument/WeekDate.php
 create mode 100644 core/modules/datetime/src/Plugin/views/argument/YearMonthDate.php

diff --git a/core/modules/datetime/datetime.views.inc b/core/modules/datetime/datetime.views.inc
index c12a3aca42a8..d3b0d18617d0 100644
--- a/core/modules/datetime/datetime.views.inc
+++ b/core/modules/datetime/datetime.views.inc
@@ -26,8 +26,11 @@ function datetime_field_views_data(FieldStorageConfigInterface $field_storage) {
     $arguments = [
       // Argument type => help text.
       'year' => t('Date in the form of YYYY.'),
-      'month' => t('Date in the form of MM.'),
-      'day' => t('Date in the form of DD.'),
+      'month' => t('Date in the form of MM (01 - 12).'),
+      'day' => t('Date in the form of DD (01 - 31).'),
+      'week' => t('Date in the form of WW (01 - 53).'),
+      'year_month' => t('Date in the form of YYYYMM.'),
+      'full_date' => t('Date in the form of CCYYMMDD.'),
     ];
     foreach ($arguments as $argument_type => $help_text) {
       $data[$table_name][$field_storage->getName() . '_value_' . $argument_type] = [
diff --git a/core/modules/datetime/src/Plugin/views/argument/FullDate.php b/core/modules/datetime/src/Plugin/views/argument/FullDate.php
new file mode 100644
index 000000000000..16644a9f7da1
--- /dev/null
+++ b/core/modules/datetime/src/Plugin/views/argument/FullDate.php
@@ -0,0 +1,22 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\datetime\Plugin\views\argument\FullDate.
+ */
+
+namespace Drupal\datetime\Plugin\views\argument;
+
+/**
+ * Argument handler for a full date (CCYYMMDD).
+ *
+ * @ViewsArgument("datetime_full_date")
+ */
+class FullDate extends Date {
+
+  /**
+   * {@inheritdoc}
+   */
+  protected $argFormat = 'Ymd';
+
+}
diff --git a/core/modules/datetime/src/Plugin/views/argument/WeekDate.php b/core/modules/datetime/src/Plugin/views/argument/WeekDate.php
new file mode 100644
index 000000000000..6b0d8175aceb
--- /dev/null
+++ b/core/modules/datetime/src/Plugin/views/argument/WeekDate.php
@@ -0,0 +1,22 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\datetime\Plugin\views\argument\WeekDate.
+ */
+
+namespace Drupal\datetime\Plugin\views\argument;
+
+/**
+ * Argument handler for a week.
+ *
+ * @ViewsArgument("datetime_week")
+ */
+class WeekDate extends Date {
+
+  /**
+   * {@inheritdoc}
+   */
+  protected $argFormat = 'W';
+
+}
diff --git a/core/modules/datetime/src/Plugin/views/argument/YearMonthDate.php b/core/modules/datetime/src/Plugin/views/argument/YearMonthDate.php
new file mode 100644
index 000000000000..1a533bf9fc53
--- /dev/null
+++ b/core/modules/datetime/src/Plugin/views/argument/YearMonthDate.php
@@ -0,0 +1,22 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\datetime\Plugin\views\argument\YearMonthDate.
+ */
+
+namespace Drupal\datetime\Plugin\views\argument;
+
+/**
+ * Argument handler for a year plus month (CCYYMM).
+ *
+ * @ViewsArgument("datetime_year_month")
+ */
+class YearMonthDate extends Date {
+
+  /**
+   * {@inheritdoc}
+   */
+  protected $argFormat = 'Ym';
+
+}
diff --git a/core/modules/datetime/src/Tests/Views/ArgumentDateTimeTest.php b/core/modules/datetime/src/Tests/Views/ArgumentDateTimeTest.php
index e40868effd1e..ba90110791e5 100644
--- a/core/modules/datetime/src/Tests/Views/ArgumentDateTimeTest.php
+++ b/core/modules/datetime/src/Tests/Views/ArgumentDateTimeTest.php
@@ -138,4 +138,71 @@ public function testDatetimeArgumentAll() {
     $view->destroy();
   }
 
+  /**
+   * Test week WW argument.
+   */
+  public function testDatetimeArgumentWeek() {
+    $view = Views::getView('test_argument_datetime');
+    // The 'embed_4' display has WW argument.
+    $view->setDisplay('embed_4');
+
+    $this->executeView($view, ['41']);
+    $expected = [];
+    $expected[] = ['nid' => $this->nodes[0]->id()];
+    $expected[] = ['nid' => $this->nodes[1]->id()];
+    $this->assertIdenticalResultset($view, $expected, $this->map);
+    $view->destroy();
+
+    $view->setDisplay('embed_4');
+    $this->executeView($view, ['01']);
+    $expected = [];
+    $expected[] = ['nid' => $this->nodes[2]->id()];
+    $this->assertIdenticalResultset($view, $expected, $this->map);
+    $view->destroy();
+  }
+
+  /**
+   * Test full_date CCYYMMDD argument.
+   */
+  public function testDatetimeArgumentFullDate() {
+    $view = Views::getView('test_argument_datetime');
+    // The 'embed_5' display has CCYYMMDD argument.
+    $view->setDisplay('embed_5');
+
+    $this->executeView($view, ['20001010']);
+    $expected = [];
+    $expected[] = ['nid' => $this->nodes[0]->id()];
+    $this->assertIdenticalResultset($view, $expected, $this->map);
+    $view->destroy();
+
+    $view->setDisplay('embed_5');
+    $this->executeView($view, ['20020101']);
+    $expected = [];
+    $expected[] = ['nid' => $this->nodes[2]->id()];
+    $this->assertIdenticalResultset($view, $expected, $this->map);
+    $view->destroy();
+  }
+
+  /**
+   * Test year_month CCYYMM argument.
+   */
+  public function testDatetimeArgumentYearMonth() {
+    $view = Views::getView('test_argument_datetime');
+    // The 'embed_6' display has CCYYMM argument.
+    $view->setDisplay('embed_6');
+
+    $this->executeView($view, ['200010']);
+    $expected = [];
+    $expected[] = ['nid' => $this->nodes[0]->id()];
+    $this->assertIdenticalResultset($view, $expected, $this->map);
+    $view->destroy();
+
+    $view->setDisplay('embed_6');
+    $this->executeView($view, ['200201']);
+    $expected = [];
+    $expected[] = ['nid' => $this->nodes[2]->id()];
+    $this->assertIdenticalResultset($view, $expected, $this->map);
+    $view->destroy();
+  }
+
 }
diff --git a/core/modules/datetime/tests/modules/datetime_test/test_views/views.view.test_argument_datetime.yml b/core/modules/datetime/tests/modules/datetime_test/test_views/views.view.test_argument_datetime.yml
index 7f3abec2292e..24900e6a0871 100644
--- a/core/modules/datetime/tests/modules/datetime_test/test_views/views.view.test_argument_datetime.yml
+++ b/core/modules/datetime/tests/modules/datetime_test/test_views/views.view.test_argument_datetime.yml
@@ -97,3 +97,45 @@ display:
     id: embed_2
     display_title: ''
     position: null
+  embed_4:
+    display_options:
+      defaults:
+        arguments: false
+      arguments:
+        field_date_value_day:
+          field: field_date_value_week
+          id: field_date_value
+          table: node__field_date
+          plugin_id: datetime_week
+    display_plugin: embed
+    id: embed_4
+    display_title: ''
+    position: null
+  embed_5:
+    display_options:
+      defaults:
+        arguments: false
+      arguments:
+        field_date_value_day:
+          field: field_date_value_full_date
+          id: field_date_value
+          table: node__field_date
+          plugin_id: datetime_full_date
+    display_plugin: embed
+    id: embed_5
+    display_title: ''
+    position: null
+  embed_6:
+    display_options:
+      defaults:
+        arguments: false
+      arguments:
+        field_date_value_day:
+          field: field_date_value_year_month
+          id: field_date_value
+          table: node__field_date
+          plugin_id: datetime_year_month
+    display_plugin: embed
+    id: embed_6
+    display_title: ''
+    position: null
-- 
GitLab