1

I have been working with Drupal 8 and need help: I have a News content type, and an Editor role type. This Editor role can view/edit/delete his own content. I want that Editor roles can't edit/delete their own content when this content is published by Admin roles.

I have been searched and I found Permissions by fields, Permissions by terms, Groups and Custom Publish options modules, but I can't make this works.

Maybe this can be done with php, but I don't know how.

Please, any help?

1

1 Answer 1

1

You could use the following code.

use Drupal\Core\Access\AccessResult;
use Drupal\node\NodeInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\user\Entity;

function mymodule_node_access(NodeInterface $node, $op, AccountInterface $account) {
  $type = $node->getType();
  // Is it published?
  $nodeIsPublished = $node->isPublished();
  if ($type == 'news' && $operation != 'view' && $nodeIsPublished == 1) {
    if($account->hasRole('rolename')) {
       return  AccessResult::forbidden();
    }
    else {
      return AccessResult::allowed();
    }
  }
  return AccessResult::neutral();
}

Also, the Override Node Options module allows permissions to be set for each field within the Authoring information and Publishing options fieldsets on the node form.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.