Problem/Motivation

The datetime module has recently been made views compatible (nice) but you can only use it to match exact dates.
There are currently 3 different date arguments ("Date in the form of YYYY.", "Date in the form of MM.", "Date in the form of DD."). They seem to be following the conventions that were set forth in the created date arguments.

Proposed resolution

Add an argument that allows filtering using operators like in the datetime filter.

Remaining tasks

Discuss best approach, write tests+code, evaaluate code.

User interface changes

Adds a datetime contextual filter that allows operator selection.

API changes

Possible deprecating current views arguments if a solution seems to make them redundant.

Data model changes

None.

Comments

Jaesin’s picture

Status: Needs work » Needs review
StatusFileSize
new14.42 KB

This is my first approach but I think it would be better to be able to handle YYYY, MM and DD in a new argument named datetime so we can deprecate the other 3 arguments.

mpdonadio’s picture

Category: Task » Feature request
Status: Needs review » Needs work
Issue tags: +VDC, +Needs beta evaluation
Related issues: +#2489476: [PP-1] Base fields miss out on Views data from hook_field_views_data()

Thanks for the initial patch. Changing this to a feature request, as it wasn't present in Drupal 7 / Views 3. I am really surprised I have never run into this situation before, but I see how this can be useful.

I don't think we would be able to deprecate the other arguments, as I suspect that would be an upgrade and migrate path nightmare and we don't want any API changes at this point. Also, in theory, we should be able to add validators to those to range check the existing ones. Instead, I would remove the fixed date option, and just have this handle computed dates (what you called offset dates).

#2489476: [PP-1] Base fields miss out on Views data from hook_field_views_data() may (or may not) complicate this issue, or change its approach.

Computed dates in the URL also require adding a cache context (probably to make it non-cachable). Not sure how to handle that with an argument plugin.

Added a few tags. We will eventually need a Beta Eval, but not until we iron this out a bit more.

  1. +++ b/core/modules/datetime/datetime.views.inc
    @@ -28,6 +28,7 @@ function datetime_field_views_data(FieldStorageConfigInterface $field_storage) {
           'day' => t('Date in the form of DD.'),
    +      'calculated' => t('Calculated Date.'),
         ];
         foreach ($arguments as $argument_type => $help_text) {
    

    Do we use the term "calculated" or "computed" more in core? I think "computed" may be more appropriate in this case.

  2. +++ b/core/modules/datetime/src/Plugin/views/argument/Date.php
    @@ -7,7 +7,7 @@
     
    -use Drupal\views\Plugin\views\argument\Date as NumericDate;
    +use Drupal\views\Plugin\views\argument\Date as FormulaicDate;
     
    

    Even though this is just an alias, I think this is too disruptive. I would rename your new class "ComputedDate" to match the argument key. This makes even more sense if you just have it handle computed dates.

  3. +++ b/core/modules/datetime/src/Plugin/views/argument/NumericDate.php
    @@ -0,0 +1,332 @@
    +  // Flag to allow the use of date offsets for default arguments.
    +  public $canUseOffsetDate = TRUE;
    +
    

    Bad docblock format. Does it really need to be public or should you have a getter instead? I also think the general convention in Drupal is not set the initial value here, but in the constructor.

  4. +++ b/core/modules/datetime/src/Plugin/views/argument/NumericDate.php
    @@ -0,0 +1,332 @@
    +
    +  function title() {
    +    $title = parent::title();
    

    Needs docblock and access declaration. This happens several times in the patch.

  5. +++ b/core/modules/datetime/src/Plugin/views/argument/NumericDate.php
    @@ -0,0 +1,332 @@
    +
    +    public function query($group_by = FALSE) {
    +    $this->ensureMyTable();
    

    Bad indent. Needs docblock.

  6. +++ b/core/modules/datetime/src/Plugin/views/argument/NumericDate.php
    @@ -0,0 +1,332 @@
    +  /**
    +   * Options form subform for setting the operator.
    +   *
    +   * This may be overridden by child classes, and it must
    +   * define $form['operator'];
    +   *
    +   * @see buildOptionsForm()
    +   */
    +  protected function operatorForm(&$form, FormStateInterface $form_state) {
    +    $options = $this->operatorOptions();
    

    Needs @param in the docblock.

Jaesin’s picture

@mpdonadio Thanks for reviewing.

Some outstanding questions.

  1. How useful are the arguments as they are now?
  2. Could all date arguments benefit by being able to use operators?
  3. Should there be an interface for implementing operators for views handlers?

I changed NumericDate to FormulaicDate because Drupal\views\Plugin\views\argument\Date extends Formula. This first patch is just meant to be a proof of concept. I want to get conversation started about this.

I would argue that the YYYY, MM and DD arguments should be able to use operators as well. If all date arguments are computable, with = being the default operator, all we need to add is a full date version.

I find the way these dates were implemented to be really weird. Does anyone know the theory behind having individual segments for date arguments but not having a complete date as a single argument? Does anyone know why operators weren't already introduced for date arguments?

mpdonadio’s picture

As for how useful, I use the YYYY and MM arguments all the time when building things like blogs, and need yearly and/or monthly archives. I've used DD once or twice.

I'm sure you can come up for a use case for using operators for computed date arguments, but in all of the cases I could think of, an exposed filter fit the bill better on sites I have done. I'm gong to let others chime in, though. I'm mostly neutral on this as is. The VDC people should see this, too.

If we do want this feature, I think adding operators to the existing arguments and adding a full date argument w/ operators may be best. Adding operators would require an upgrade path + tests, as it would be a schema change.

Allowing operators on date arguments from the URL has caching complications, and D7 and earlier made this tougher to handle in a sane manner. With exposed filters, the operator becomes part of the URL, so you don't have the problem.

As for why things were implemented the way they were, I don't think we will ever know other than to say that both Views and DateTime were non-core modules before Drupal 8, and a lot of features were straight ports and relatively unchanged from Drupal 5.

amateescu’s picture

Discussed this with @dawehner and it's very unlikely to happen for core 8.0.0, so we should probably move it to the Date contrib module and try to bring it into core in 8.1.

mpdonadio’s picture

Version: 8.0.x-dev » 8.1.x-dev
Status: Needs work » Postponed
Issue tags: -Needs beta evaluation

Still not 100% convinced this is needed, especially with the caching complications, but yeah, let's postpone for now.

luksak’s picture

I just came across this because I need a year based exposed filter. Should filters be covered in a different issue? In my opinion Views integration is incomplete if this is missing.

Jaesin’s picture

Hello @Lukas, I agree with you and think there is no reason postpone development on this issue if anyone has time to work on it.

From my perspective, it makes sense to add operators to the base class so you can choose the operator (defaulting to "=") for all date arguments.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.0-beta1 was released on March 2, 2016, which means new developments and disruptive changes should now be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

caspervoogt’s picture

Just chiming in to say that filtering on year, month, and day separately would be great. In D7 this was possible by setting the granularity in the filter's settings. I am really missing that feature. Perhaps the Date module will finally handle this and get rolled into core.

sukanya.ramakrishnan’s picture

+1 for caspervoogt's comment!!! Really missing the granularity feature!! Hope it gets added to core soon!

mpdonadio’s picture

Status: Postponed » Needs work

There is not reason to postpone this as far as I can tell. However, we are realling trying to move away from the do-it-all plugins in 8.x, and breaking things down into more manageable plugins. TBH, I need to review the IS and the plugins to assess where we stand with this.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

jhedstrom’s picture

Issue tags: +Needs issue summary update
StatusFileSize
new120.04 KB

I've never been quite clear on what this issue was about. From the IS:

but you can only use it to match exact dates.

This isn't accurate. The views integration went in with the same granularity that numeric dates receive from the Views module itself. Specifically, it provides individual date components (year, month, day), and some commonly used combinations (year & month for instance).

Here's the list of available arguments for a single field:

Marking as needing an IS update to clarify what would need to be done.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

benoitf’s picture

Maths comparison and date offset would be much more pertinent than string tools (begin with, string length).

Could date fields be managed as the publication date?
Filtering on publication date is ok.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

jibran’s picture

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.

smustgrave’s picture

Status: Needs work » Postponed (maintainer needs more info)
Issue tags: +stale-issue-cleanup

Thank you for sharing your idea for improving Drupal.

We are working to decide if this proposal meets the Criteria for evaluating proposed changes. There hasn't been any discussion here for over 8 years which suggests that this has either been implemented or there is no community support. Your thoughts on this will allow a decision to be made.

Since we need more information to move forward with this issue, the status is now Postponed (maintainer needs more info). If we don't receive additional information to help with the issue, it may be closed after three months.

Thanks!

benoitf’s picture

I've just checked, and date comparison is now possible in Views. I've managed to filter a view with dates (either a publication date or a content date), with operators (>, <, etc.) on date offset (eg "-1 month") or YYYY-MM-DD.

I don't how long this is possible, but I can now replace my workarounds! ;-)

So, I think this issue can be closed.