I use content moderation, recently present in drupal 8 core, for editorial workflow.

I'd like to prevent the entity from being saved in an update if the current user is the author of the document.

how could I achieve that ?

this does a redirection but the entity saves still does happen

 function hook_entity_presave(Drupal\Core\Entity\EntityInterface $entity) {

 if ($entity->getEntityTypeId() !== 'content_moderation_state' ) {

$article = \Drupal\node\Entity\Node::load($article_id);
 if($article->bundle()=='article') {


    $moderation_state = $entity->get('moderation_state')->getString();

    switch ($moderation_state) {
        case 'peer-reviewing':


            //$entity= $entity->original;
             //if author same as current user:
            drupal_set_message(t('original author cannot be reviewer.'), 'error');
            $url = $base_url.\Drupal::service('path.current')->getPath();
            $response = new Symfony\Component\HttpFoundation\RedirectResponse($url);

  • 1
    If you want to prevent that the entity is saved, you should throw an exception. But it is more user friendly to do this in the form, see the answer from @Kevin.
    – 4uk4
    Commented Sep 21, 2016 at 16:54
  • is it possible to throw an exception and render the layout page with css (not a WSOD)?
    – Matoeil
    Commented Sep 22, 2016 at 9:20
  • To do this, put a validate handler in hook_form_alter.
    – 4uk4
    Commented Sep 22, 2016 at 9:41

2 Answers 2


Could you not instead implement a hook_form_alter and do your condition checks, and hide/remove the submit button if it is the same user trying to review?

You could set the same message at the top of the form and not have to do any redirects.

  • thanks that's an idea but i dont know if i can retrieve the moderation state of the article from hook_form_alter ?
    – Matoeil
    Commented Sep 21, 2016 at 16:00
  • I don't have a way to scratch up code offhand, but I think you should be able to fetch the entity.
    – Kevin
    Commented Sep 21, 2016 at 16:01
  • i did not find how
    – Matoeil
    Commented Sep 21, 2016 at 16:34
  • 2
    you can get the entity with: $form_state->getFormObject()->getEntity()
    – 4uk4
    Commented Sep 21, 2016 at 16:52

how i did it:

function my_module_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id){
  if ($form_id='node_article_edit_form')

    //get the associated content moderation entity
    $id = Drupal::entityQuery('content_moderation_state')
       ->condition('content_entity_id', $article->id())

    $stateEntities = Drupal::entityManager()
    $moderation_state = $stateEntity->get('moderation_state')->getString();

        $form['#validate'][] = 'my_module_node_article_edit_form_validate';

function my_module_node_article_edit_form_validate(&$form, \Drupal\Core\Form\FormStateInterface $form_state) {

if(($author_name==$current_user_name) && ($current_user_name!='admin'))
    $error_msg="En tant qu'auteur de l'article, ".$current_user_name.", vous ne pouvez être relecteur.";
    $form_state->setErrorByName('user_moderation_constraint', $error_msg);



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.