0

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' ) {
    return;
}

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

    $author_name=$article->getOwner()->getAccountName();
    $current_user_name=\Drupal::currentUser()->getAccountName();

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

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

           $author=$article->getOwner();
           $current_user=\Drupal::currentUser();

            //$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);
            $response->send();
            return;

 }
3
  • 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
    Sep 21, 2016 at 16:54
  • is it possible to throw an exception and render the layout page with css (not a WSOD)?
    – Matoeil
    Sep 22, 2016 at 9:20
  • To do this, put a validate handler in hook_form_alter.
    – 4uk4
    Sep 22, 2016 at 9:41

2 Answers 2

3

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.

4
  • thanks that's an idea but i dont know if i can retrieve the moderation state of the article from hook_form_alter ?
    – Matoeil
    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
    Sep 21, 2016 at 16:01
  • i did not find how
    – Matoeil
    Sep 21, 2016 at 16:34
  • 2
    you can get the entity with: $form_state->getFormObject()->getEntity()
    – 4uk4
    Sep 21, 2016 at 16:52
2

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')
{

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

    $stateEntities = Drupal::entityManager()
        ->getStorage('content_moderation_state')
        ->loadMultiple($id);
    $stateEntity=array_values($stateEntities)[0];
    $moderation_state = $stateEntity->get('moderation_state')->getString();

    if($moderation_state=='review){
        $form['#validate'][] = 'my_module_node_article_edit_form_validate';
    }
}

function my_module_node_article_edit_form_validate(&$form, \Drupal\Core\Form\FormStateInterface $form_state) {
$article=$form_state->getFormObject()->getEntity();
$author_name=$article->getOwner()->getAccountName();
$current_user_name=\Drupal::currentUser()->getAccountName();

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);
}

return;

}

Your Answer

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

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