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.