View source
<?php
namespace Drupal\contextual_range_filter\Plugin\views\argument;
use Drupal\views\ViewExecutable;
use Drupal\views\Plugin\views\argument\Date;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\contextual_range_filter\ContextualRangeFilter;
class DateRange extends Date {
use MultiRangesTrait;
public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
parent::init($view, $display, $options);
switch ($options['id']) {
case 'changed_year':
case 'created_year':
$this->format = 'Y';
$this->argFormat = 'Y';
break;
case 'changed_year_month':
case 'created_year_month':
$this->format = 'F Y';
$this->argFormat = 'Ym';
break;
case 'changed_month':
case 'created_month':
$this->format = 'F';
$this->argFormat = 'm';
break;
case 'changed_week':
case 'created_week':
$this->format = 'w';
$this->argFormat = 'W';
break;
case 'changed_day':
case 'created_day':
$this->format = 'j';
$this->argFormat = 'd';
break;
default:
$this->format = 'F j, Y';
$this->argFormat = 'Y-m-d';
break;
}
}
protected function defineOptions() {
$options = parent::defineOptions();
$options['relative_dates'] = [
'default' => FALSE,
];
$options['break_phrase'] = [
'default' => FALSE,
];
$options['not'] = [
'default' => FALSE,
];
return $options;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
$form['description']['#markup'] = t('Contextual date range filter values are taken from the URL.');
$form['more']['#open'] = TRUE;
$form['relative_dates'] = [
'#type' => 'checkbox',
'#title' => t('Allow relative date ranges'),
'#description' => t('If ticked, offsets from the current date may be specified.<br/>Example: <strong>2 weeks ago--yesterday"</strong>'),
'#default_value' => $this->options['relative_dates'],
'#group' => 'options][more',
];
$form['break_phrase'] = [
'#type' => 'checkbox',
'#title' => t('Allow multiple date ranges'),
'#description' => t('If selected, multiple date ranges may be specified by stringing them together with plus signs.<br/>Example: <strong>19990101--20051231+20130701--20140630</strong>'),
'#default_value' => $this->options['break_phrase'],
'#group' => 'options][more',
];
$form['not'] = [
'#type' => 'checkbox',
'#title' => t('Exclude'),
'#description' => t('Negate the range. If selected, output matching the specified date range(s) will be excluded, rather than included.'),
'#default_value' => !empty($this->options['not']),
'#group' => 'options][more',
];
}
public function title() {
if (!$this->argument) {
return $this->definition['empty field name'] ?: t('Uncategorized');
}
if (!empty($this->options['break_phrase'])) {
$this
->breakPhraseRange($this->argument);
}
else {
$this->value = [
$this->argument,
];
$this->operator = 'or';
}
if ($this->value === FALSE) {
return $this->definition['invalid input'] ?: t('Invalid input');
}
if (empty($this->value)) {
return $this->definition['empty field name'] ?: t('Uncategorized');
}
return implode($this->operator == 'or' ? ' + ' : ', ', $this->value);
}
public function getDateField() {
$first7chars = substr($this->field, 0, 7);
$is_string_date = $first7chars != 'changed' && $first7chars != 'created' && $this->field != 'login' && $this->field != 'access';
return $this->query
->getDateField("{$this->tableAlias}.{$this->realField}", $is_string_date);
}
public function query($group_by = FALSE) {
$this
->ensureMyTable();
if (!empty($this->options['break_phrase'])) {
$this
->breakPhraseRange($this->argument);
}
else {
$this->value = [
$this->argument,
];
}
$formula = $this
->getFormula();
$range_conversion = empty($this->options['relative_dates']) ? NULL : [
$this,
'convertRelativeDateRange',
];
ContextualRangeFilter::buildRangeQuery($this, $formula, $range_conversion);
}
public function convertRelativeDateRange($from, $to) {
$format = $this->argFormat;
if (!empty($from)) {
$abs_from = strtotime($from);
$from = empty($abs_from) ? date($format) : date($format, $abs_from);
}
if (!empty($to)) {
$abs_to = strtotime($to);
$to = empty($abs_to) ? date($format) : date($format, $abs_to);
}
return [
$from,
$to,
];
}
}