Last active
August 25, 2017 11:54
-
-
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.
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
<?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