Ho dovuto filtrare un tipo di contenuto contenente le date delle aste in tre gruppi (ora online, in arrivo e pronto per la registrazione). Dato che ciò ha comportato molteplici problemi, ho scritto un plug-in basato su questo articolo: https://www.webomelette.com/creating-custom-views-filter-drupal-8
Fondamentalmente fa tre cose: - Convertire le date nel campo in data e ora locali. - Il filtro ha tre possibili impostazioni 'now online', 'coming' e 'other' - In base alle impostazioni aggiunge le possibili clausole where alla query
Funziona e sembra robusto agli aggiornamenti futuri.
<?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;
}
}
}