Last active
October 14, 2019 10:12
-
-
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
This file contains 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
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