I need to filter a node by moderation state. I am using the Drupal 8 content moderation module.

I basically want to get NIDs of all nodes in draft state. Something similar to this.

$nids = \Drupal::entityQuery('node')
      ->condition('moderation_state', 'draft')

You try the code :

$query = \Drupal::database()->select('content_moderation_state_field_data', 'ms');
    $query->join('node_field_revision', 'nf', 'nf.nid=ms.content_entity_id AND nf.vid=ms.content_entity_revision_id AND nf.langcode=ms.langcode');
    $results = $query->execute()->fetchAllAssoc('content_entity_id');

Because entityQuery not filter content moderation


Posting a link to the d.o issue queue where as of this week a potential patch to core to allow EntityQuery to recognize Workflow moderation state that @Santiago cited above: https://www.drupal.org/project/drupal/issues/3025164#comment-13221005 Without this patch or a similar core hack, EntityQuery can not query Drupal 8 Workflow moderation state.

Your Answer

