Saya harus memfilter tipe konten yang berisi tanggal lelang ke dalam tiga grup (sekarang online, akan datang dan siap mendaftar). Karena ini melibatkan banyak daterange, saya menulis sebuah plugin berdasarkan artikel ini: https://www.webomelette.com/creating-custom-views-filter-drupal-8
Pada dasarnya adalah melakukan tiga hal: - Mengkonversi tanggal di lapangan ke tanggal dan waktu setempat. - Filter memiliki tiga kemungkinan pengaturan 'sekarang online', 'akan datang' dan 'lainnya' - Berdasarkan pengaturan itu menambahkan kemungkinan klausa mana ke kueri
Ini berfungsi dan tampaknya kuat untuk pembaruan di masa mendatang.
<?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;
}
}
}