8

In Drupal 8, I have a view that gathers all the nodes built in my Gallery Hours content type. The content type has a field called View Order that I use for another view that displays the list of gallery hours in that order. What I am trying to do is to build another view that checks for the gallery hours for today. For instance, today is Tuesday, so I would like the view to display the gallery hours for Tuesday.

My thought was to create a custom filter option that compares the numeric value of a field, such as my View Order field which is set to 1=Sunday, 2=Monday, etc., with DATE_FORMAT(NOW(),"%N"). This is my first foray into custom Drupal module development and am spinning my wheels. Any and all suggestions welcome.

The one thing I think I have correct are my yaml files.

todaysdate.info.yml

name: Todays Date Filter module
description: 'Creates a filter to search by todays day of the week.'
type: module
core: 8.x
package: SMA Modules

todaysdate.routing.yml

todaysdate:
  defaults:
    _controller: Drupal\todaysdate\Plugin\Filter\todays_date_handler_filter_numeric::operators
  requirements:
    _permission: 'access content'

Where I am really confused is where to go from there. I am trying to piece together a class that extends the NumericFilter and add a new operator that then add to the WHERE expression with the dateformat I'm looking for.

Anyhoo, this is my attempt at creating the filter. I am not getting any errors, but I am also not getting the new operator on my numeric fields in my view.

todays_date_handler_filter_numeric.php

<?php

/**
 * @file
 * Provides views filter option for the Todays Date module.
 */

namespace Drupal\todaysdate\Plugin\Filter;

use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\views\Plugin\views\filter\NumericFilter;
use Drupal\views\Plugin\views\filter\InOperator;

class todays_date_handler_filter_numeric extends NumericFilter {
  function operators(){
    $operators = parent::operators();
    $operators += array(
      'todays date' => array(
        'title' => $this->t('Is this day of the week'),
        'short' => $this->t('todays date'),
        'method' => 'op_day_of_week',
        'values' => 0,
      )
    );
    return $operators;

    function op_day_of_week($field) {
      $this->query->addWhereExpression($this->options['group'], 'DATE_FORMAT(NOW(),"%N")');
    }
  }
}

Thank you all in advance for any assistance you can provide. I can't tell you how many times StackOverflow has helped me in the past.

3
  • Kummerow can you accept the answer by @Gun5m0k3 below? I want to mark [220129] as a duplicate of this question.
    – Darvanen
    Commented Sep 23, 2017 at 7:30
  • @Darvanen, upvotes help too
    – 4uk4
    Commented Sep 27, 2017 at 14:58
  • That was my first action ;)
    – Darvanen
    Commented Sep 27, 2017 at 22:55

1 Answer 1

4

You should register your custom filter in both mymodule.views.inc and in the class annotations.

\Drupal\mymodule\Plugin\views\filter\MyCustomNumericFilter

<?php

namespace Drupal\mymodule\Plugin\views\filter;

use Drupal\views\Plugin\views\filter\NumericFilter;

/**
 * My custom numeric filter.
 *
 * @ingroup views_filter_handlers
 *
 * @ViewsFilter("my_custom_numeric_filter")
 */
class MyCustomNumericFilter extends NumericFilter {

  public function query() {   
    $this->query->addWhereExpression($this->options['group'], 'DATE_FORMAT(NOW(),"%N")');
  }

  protected function operators() {
    $operators = parent::operators();
    $operators += array(
      'todays date' => array(
        'title' => $this->t('Is this day of the week'),
        'short' => $this->t('todays date'),
        'method' => 'op_day_of_week',
        'values' => 0,
      )
    );
    return $operators;
  }

}

mymodule.views.inc (placed in custom module root)

function mymodule_views_data() {
  $data = [];
  $data['views']['my_custom_numeric_filter'] = [
    'title' => t('My custom numeric filter'),
    'filter' => [
      'title' => t('My custom numeric filter'),
      'group' => 'Custom',
      'help' => t('Provides custom numeric filter.'),
      'id' => 'my_custom_numeric_filter'
    ],
  ];
  return $data; 
}

I moved your custom query expression (without checking) to the query() method since Views calls this method on filtering.

Let me know if this works for you.

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.