ViewsSearchApiYearFilterDate.php in Views year filter 8
File
src/Plugin/views/filter/ViewsSearchApiYearFilterDate.php
View source
<?php
namespace Drupal\views_year_filter\Plugin\views\filter;
use Drupal\Core\Form\FormStateInterface;
use Drupal\search_api\Plugin\views\filter\SearchApiDate;
use Drupal\views_year_filter\DateViewsTrait;
class ViewsSearchApiYearFilterDate extends SearchApiDate {
use DateViewsTrait;
protected function valueForm(&$form, FormStateInterface $form_state) {
parent::valueForm($form, $form_state);
if (!$form_state
->get('exposed')) {
$form['value']['type'] = [
'#type' => 'radios',
'#title' => $this
->t('Value type'),
'#options' => [
'date' => $this
->t('A date in any machine readable format. CCYY-MM-DD HH:MM:SS is preferred.'),
'date_year' => $this
->t('A date in yyyy format.'),
'offset' => $this
->t('An offset from the current time such as "@example1" or "@example2"', [
'@example1' => '+1 day',
'@example2' => '-2 hours -30 minutes',
]),
],
'#default_value' => isset($this->value['type']) ? $this->value['type'] : 'date',
];
$form['#attached']['library'][] = 'views_year_filter/year_filter';
}
}
protected function opSimple($field) {
if (!empty($this->value['type']) && $this->value['type'] == 'date_year' && isset($this->value['value'])) {
$startDate = intval($this->value['value']) . '-01-01 00:00:01';
$endDate = intval($this->value['value']) . '-12-31 23:59:59';
$this
->getQuery()
->addCondition($this->realField, [
strtotime($startDate),
strtotime($endDate),
], 'BETWEEN', $this->options['group']);
}
else {
parent::opSimple($field);
}
}
public function buildExposedForm(&$form, FormStateInterface $form_state) {
parent::buildExposedForm($form, $form_state);
$this
->applyDatePopupToForm($form);
}
}