Skip to content

Instantly share code, notes, and snippets.

@gwagroves
Last active August 25, 2017 11:54
Show Gist options
  • Save gwagroves/662ac5390160d6b5731690d3764366d3 to your computer and use it in GitHub Desktop.
Save gwagroves/662ac5390160d6b5731690d3764366d3 to your computer and use it in GitHub Desktop.
Drupal 8: Prevent node pages (full view) being shown to non-admin users.
<?php
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/**
* Implements hook_preprocess_HOOK().
*/
function my_module_preprocess_node(&$variables) {
$node = $variables['node'];
$nodetype = $node->getType();
// Only allow pages to be viewed by non-admin users:
if ($variables['view_mode'] === 'full' && !$variables['is_admin']) {
$legal = [
'page',
'node_type',
];
if (!in_array($nodetype, $legal)) {
throw new NotFoundHttpException();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment