Last active
June 22, 2018 13:14
-
-
Save DiDebru/c4037b26b04389e86cc9d08aed418260 to your computer and use it in GitHub Desktop.
node access hook content moderation and workflow
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
/** | |
* 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