Skip to content

Instantly share code, notes, and snippets.

@lincoln-chawora
Last active October 14, 2019 10:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lincoln-chawora/c32d11ea3bfd2c964eee37d69aa26a4d to your computer and use it in GitHub Desktop.
Save lincoln-chawora/c32d11ea3bfd2c964eee37d69aa26a4d to your computer and use it in GitHub Desktop.
How to exclude the current node from a view display in drupal 8
function YOUR_MODULE_views_query_alter(ViewExecutable $view, QueryPluginBase $query) {
if ($view->id() == 'name_of_your_view') {
$node = \Drupal::routeMatch()->getParameter('node');
if ($node instanceof NodeInterface) {
// You can get nid and anything else you need from the node object.
$nid = $node->id();
$query
->addWhere(0, db_and()
->condition('node_field_data.nid', $nid, '!='));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment