0

from a content moderation entity, i know how to get the associated node:

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

What i need to do is the reverse. Being able from hook_form_alter, to get the content moderation entity related to the node:

 function hook_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id){
  if ($form_id='node_article_edit_form')
  {
    $node=$form_state->getFormObject()->getEntity();
    //$node->get_content_moderation_entity??

}

}

1 Answer 1

1

i did it with entityQuery

function hook_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();
   }
 }

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.