Skip to content

Instantly share code, notes, and snippets.

@DiDebru
Last active June 22, 2018 13:14
Show Gist options
  • Save DiDebru/c4037b26b04389e86cc9d08aed418260 to your computer and use it in GitHub Desktop.
Save DiDebru/c4037b26b04389e86cc9d08aed418260 to your computer and use it in GitHub Desktop.
node access hook content moderation and workflow
/**
* Implements hook_node_access().
*/
function my_module_node_access(\Drupal\node\NodeInterface $node, $op, \Drupal\Core\Session\AccountInterface $account) {
$state = $node->moderation_state->value;
switch($op) {
case 'update':
if ($state !== 'draft') {
if ($account->hasPermission('view any unpublished content')) {
return AccessResult::allowed()->cachePerPermissions();
}
else {
// Redirect to admin/content
$url = Url::fromRoute("system.admin_content")->toString();
$response = new RedirectResponse($url);
$response->send();
// Just in case author is trying something evil.
return AccessResult::forbidden();
}
}
break;
default :
return AccessResult::neutral();
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment