How to get current content moderation state in drupal 8.

Currently upon editing a node has workflow current state displayed on the node edit page. This information will not be available on node_load or entity load.  Since the current workflow or content moderation state information will be on latest revision of that content. All just you need to do is to load the latest revision of the content.

Follow the below steps to find and load the latest revision id and load the same to identify current workflow state in Drupal 8. Lets consider a "Node" which has workflow configured.

Loading a Node using node_load:

$node = node_load('507'); // where 507 is the Node Id
$node_vid = \Drupal::entityManager()->getStorage('node')->revisionIds($node); // Get list of Revision Ids available to that node. Return type will an array.
$latest_revision_id = array_pop((array_slice($node_vid, -1))); // Getting the last element of the array which is the latest Revision Id.


Using node_revision_load function:

$status = node_revision_load($latest_revision_id)->get('moderation_state')->getValue(); // node_revision_load is the function. 'moderation_state' is the field containing the current workflow state.


$node = node_load('507');
$node_vid = \Drupal::entityManager()->getStorage('node')->revisionIds($node);
$latest_revision_id = array_pop((array_slice($node_vid, -1)));
$current_state = node_revision_load($latest_revision_id)->get('moderation_state')->getValue()[0]['value'];
print_r('The current moderation state is in ' . $current_state);


The current moderation state is in Review