경매 날짜가 포함 된 컨텐츠 유형을 세 그룹 (현재 온라인, 곧 예정된 등록 준비)으로 필터링해야했습니다. 이것은 여러 날짜 범위와 관련되어 있으므로이 기사를 기반으로 플러그인을 작성했습니다 : https://www.webomelette.com/creating-custom-views-filter-drupal-8
기본적으로 세 가지 작업이 수행됩니다.-필드의 날짜를 현지 날짜 및 시간으로 변환합니다. -필터에는 '지금 온라인', '다가오는'및 '기타'의 세 가지 가능한 설정이 있습니다.-설정에 따라 쿼리에 가능한 where-clauses를 추가합니다.
작동하며 향후 업데이트에 강력 해 보입니다.
<?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;
}
}
}