Created
December 11, 2021 20:13
-
-
Save andypost/34ff91c12846a7af442f7f84e1e2fc49 to your computer and use it in GitHub Desktop.
Views date fielter plugin alter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: My date | |
description: "Overrides core date views filter" | |
type: module | |
package: "Views" | |
core_version_requirement: '>=8.8.0' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// overrides views date plugin | |
use Drupal\mydate\Plugin\views\filter\MyDate; | |
/** | |
* Implements hook_views_plugins_filter_alter(). | |
*/ | |
function mydate_views_plugins_filter_alter(array &$plugins) { | |
// Change the 'date' handler class. | |
$plugins['date']['class'] = MyDate::class; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Drupal\mydate\Plugin\views\filter; | |
use Drupal\Core\Form\FormStateInterface; | |
use Drupal\views\Plugin\views\filter\Date; | |
/** | |
* Filter to handle dates stored as a timestamp, extended by "aday" option. | |
* | |
* @ingroup views_filter_handlers | |
*/ | |
class MyDate extends Date { | |
/** | |
* {@inheritdoc} | |
*/ | |
protected function valueForm(&$form, FormStateInterface $form_state) { | |
parent::valueForm($form, $form_state); | |
if (isset($form['value']['type']['#options'])) { | |
$form['value']['type']['#options']['aday'] = $this->t('One day'); | |
} | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
protected function opSimple($field) { | |
if ($this->value['type'] === 'aday') { | |
$value = $this->value['value']; | |
// @todo Convert input to unixtime in UTC timezone and use between or | |
// (field > $a and $field < $b) where a and b are daily bounds. | |
$this->query->addWhereExpression($this->options['group'], "$field $this->operator $value"); | |
} | |
else { | |
parent::opSimple($field); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment