21

In Drupal 7 with Views an Date module enabled, it was pretty easy to filter a view by Date: The UI just gave you the option to do so.

In Drupal 8, however, the date field and views are part of core, but if you choose a date range field as filter in views it does not offer you date specific options anymore, but shows you only filtering options for text fields: enter image description here

The date specific options, however, are available for internal date values like ´Content: Changed´ enter image description here

As I would like to show only specific nodes, with a date before or after the actual date, where the date is specified by a custom date range field, this does not suit my needs.

How can I filter a view in Drupal 8 by a date field with date specific operations?

5
  • Proper views filters are an in process issue for date ranges. The patch works, but the update path is insanely difficult.
    – mpdonadio
    Jan 28, 2017 at 15:07
  • @mpdonadio Could you help me with a link to the patch, please?
    – user5950
    Jan 28, 2017 at 15:16
  • 2
    It's drupal.org/node/2786577, but be warned the update path is not fully tested. Use this patch at your own risk.
    – mpdonadio
    Jan 28, 2017 at 15:46
  • @Pierre.Vriens it is not a duplicate. This is about a field with begin and end dates and filtering on those. Not just a single value field. Jan 28, 2017 at 18:12
  • @Neograph734 OK, that helps to better understand the differences (vote retracted). But OPer better edits this question to include that in the actual question also (to avoid others will perceive it as a duplicate) ... Good luck! Jan 28, 2017 at 18:17

6 Answers 6

14

Surprisingly this is not possible with Drupal 8 yet. But there is a long history of trying to make it work: https://www.drupal.org/node/2786577 The last patch, you find in this post, seems to work. You can download and apply it, using the following commands:

wget https://www.drupal.org/files/issues/improve_the_views-2786577-76-core.patch
git apply improve_the_views-2786577-76-core.patch

If git isn't available on your server, try:

patch -p1 < improve_the_views-2786577-76-core.patch

TO apply the patches you have to navigate to the core folder first. And be aware that you have to reapply this patch, after committing a core update. (This sucks!) So let us hope, the Drupal Gods will get this into core soon!

5
  • can u please tell me how to apply the patch its not working for me !! @user5950 Feb 2, 2017 at 11:38
  • patch -p1 < improve_the_views_integration_for_daterange_fields-2786577-60.patch worked for me Thanks @user5950 Feb 2, 2017 at 12:32
  • Make sure you apply the database updates: drush updatedb --entity-updates Mar 22, 2017 at 9:55
  • If you use composer to apply patches, they will automatically apply when you update modules. That said, this one is going to be included in 8.6 - the patch was committed a few days ago!
    – mortona42
    Mar 13, 2018 at 1:57
  • 6
    Just as a heads-up, this functionality is now in core starting with 8.6.0 Oct 4, 2018 at 11:29
1

I had to filter a content type containing the dates of auctions into three groups (now online, upcoming and ready to register). As this involved multiple daterange, I wrote a plugin based on this article: https://www.webomelette.com/creating-custom-views-filter-drupal-8

Basically is does three things: - Convert the dates in the field to local date and time. - The filter has three possible settings 'now online','upcoming' and 'other' - Based on the settings it adds the possible where-clauses to the query

It works and seems robust to future updates.

    <?php

    /**
     * @file
     * Definition of Drupal\d8views\Plugin\views\filter\NodeTitles.
     */

    namespace Drupal\d8views\Plugin\views\filter;

    use Drupal\views\Plugin\views\filter\FilterPluginBase;
    use Drupal\views\Plugin\views\filter\InOperator;
    use Drupal\views\Plugin\views\filter\ManyToOne;
    use Drupal\views\ViewExecutable;
    use Drupal\views\Views;
    /**
     * Filters by given list of node title options.
     *
     * @ingroup views_filter_handlers
     *
     * @ViewsFilter("d8views_node_titles")
     */
    class NodeTitles extends FilterPluginBase {
        // exposed filter options
        protected $alwaysMultiple = TRUE;

        /**
         * Provide simple equality operator
         */
        public function operatorOptions() {
            return [
                'nu_online' => $this->t('Nu online'),
                'binnenkort' => $this->t('Binnenkort'),
                'anders' => $this->t('Anders'),
            ];
        }


        public function query() {
            //Get the current domain.  
            //$domain = domain_get_domain();
            $nu_in_utc = new \DateTime('now', new \DateTimezone('UTC'));
            $nu_in_utc_in_iso = $nu_in_utc->format('Y-m-d\TH:i:s');
            $nu_date = $nu_in_utc->format('Y-m-d');
            /* 
            * Voeg relatie met datum veiling toe
            */
            $configuration = [
                'table'      => 'node__field_datum_veiling',
                'left_table' => 'node_field_data',
                'left_field' => 'nid',
                'field'      => 'entity_id',
                'type'       => 'LEFT',
                'extra_operator'   => 'AND',
            ];
            $join = Views::pluginManager('join')->createInstance('standard', $configuration);
            $this->query->addRelationship('node__field_datum_veiling', $join, 'node_field_data');

            /* 
            * Voeg relatie met online datum van de veiling
            */
            $configuration = [
                'table'      => 'node__field_datum_online',
                'left_table' => 'node_field_data',
                'left_field' => 'nid',
                'field'      => 'entity_id',
                'type'       => 'LEFT',
                'extra_operator'   => 'AND',
            ];

            $join = Views::pluginManager('join')->createInstance('standard', $configuration);
            $this->query->addRelationship('node__field_datum_online', $join, 'node_field_data');
            switch($this->operator) {
                case 'nu_online':
                    /* 
                    * Condities voor 'Nu online'
                    */
                    //dpm('Nu online');
                    $this->query->addWhere('AND', 'node__field_datum_veiling.field_datum_veiling_end_value', $nu_in_utc_in_iso, '>');
                    $this->query->addWhere('AND', 'node__field_datum_online.field_datum_online_value', $nu_date, '<=');
                    break;
                case 'binnenkort':
                    /* 
                    * Condities voor 'Binnenkort'
                    */
                    //dpm('Binnenkort');
                    $this->query->addWhere('AND', 'node__field_datum_veiling.field_datum_veiling_end_value', $nu_in_utc_in_iso, '>');
                    $this->query->addWhere('AND', 'node__field_datum_online.field_datum_online_value', $nu_date, '>');
                    break;
                case 'anders':
                    /* 
                    * Condities voor 'Anders' (dwz online, binnenkort, maar nog niet geweest)
                    */
                    $this->query->addWhere('AND', 'node__field_datum_veiling.field_datum_veiling_end_value', $nu_in_utc_in_iso, '>');
                    break;
            }
        }
    }
0

Using interval-filter plugin you can choose two fields to be used respectively as minimum and maximum date, this introduces the concept of range. Then you can filter the views by specifying a date that must be contained or not contained in the range.

Take a look at: https://github.com/barsan-ds/interval-filter for examples

0

Currently Views is not very aware of date ranges. It can filter on the start or end date as separate filters but not the date range as a whole.

To enable filtering based on the date range I created the module Views Date Range Filters. Currently it has 3 additional filters for Datetime Range fields:

Includes

Filters by date ranges which include the supplied date.

Overlaps

Filters by date ranges which overlap with the supplied date range.

Ends by

Filters by date ranges which end by the supplied date. Equivalent to "End date <= supplied date". Useful for grouped filters when using "Start date".

-1

Custom Views Filter Module works well for me:
https://www.drupal.org/project/custom_view_filters

It adds a date range filter to a view. See this screenshot from the project page on drupal.org.

enter image description here

-3

Date popup This module serve the purpose.

1
  • The module is good, but it only replaces the UI components, and does not allow to manage the value type.
    – milkovsky
    Aug 1, 2018 at 11:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.