I want to list (in a views block) only teasers of articles that are younger than the current node.

Somehow I have to use the current node from url->field_date for filtering. Is this possible with views via the UI (Contextual Settings)?

Or do I have to create a module for that? If so... which hook should I use.. how to tell the view to filter?

1 Answer 1


Don't think its possible with just configuration and/or contrib modules.

I believe you have to override hook_views_query_alter(&$view, &$query) and add $query->addWhereExpression based on the current nodes date field.

Dig in core/modules/datetime/src/Plugin/views/filter/Date.php function opSimple($field) and perhaps core/modules/views/src/Plugin/views/filter/Date.php function opSimple($field) to see how they properly use the addWhereExpression

  • Thanks GiorgosK. I just used the hook_views_query_alter hook. Actually the only thing I have to do is to change one value in the query: The static value I added in views I have to override: 2017-08-27T14:56:26 should become node.field_date.value. How to achieve this?
    – mogio
    Aug 27, 2018 at 16:01
  • see my revised answer otherwise you will need to do a QUICK and dirty str_replace on the $query->where array (appropriate condition)
    – GiorgosK
    Aug 27, 2018 at 16:41
  • $date = new DateTime($release_date, new DateTimeZone(Drupal::config('system.date')->get('timezone')['default'])); $value = sprintf('%d', $date->getTimestamp() + $date->getOffset()); $query->addWhereExpression(0, "node__field_release_date.field_release_date_value > $value");
    – mogio
    Aug 28, 2018 at 8:12
  • I don't seem to use the addWhereExpression function correctly
    – mogio
    Aug 28, 2018 at 8:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.