J'ai dû filtrer un type de contenu contenant les dates des enchères en trois groupes (maintenant en ligne, à venir et prêts à s'inscrire). Comme cela impliquait plusieurs daterange, j'ai écrit un plugin basé sur cet article: https://www.webomelette.com/creating-custom-views-filter-drupal-8
Fondamentalement, cela fait trois choses: - Convertir les dates du champ en date et heure locales. - Le filtre a trois paramètres possibles «maintenant en ligne», «à venir» et «autre» - En fonction des paramètres, il ajoute les clauses where possibles à la requête
Il fonctionne et semble robuste aux futures mises à jour.
<?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;
}
}
}