Ich musste einen Inhaltstyp mit den Auktionsterminen in drei Gruppen einteilen (jetzt online, in Kürze verfügbar und zur Registrierung bereit). Da es sich um mehrere Themen handelte, habe ich ein Plugin geschrieben, das auf diesem Artikel basiert: https://www.webomelette.com/creating-custom-views-filter-drupal-8
Grundsätzlich gilt Folgendes: - Konvertieren Sie die Daten im Feld in lokales Datum und Uhrzeit. - Der Filter verfügt über drei mögliche Einstellungen: "Jetzt online", "Bevorstehend" und "Andere". - Basierend auf den Einstellungen werden der Abfrage die möglichen Where-Klauseln hinzugefügt
Es funktioniert und scheint robust für zukünftige 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;
}
}
}