I'm wanting to get the last published revision of a node that has a content moderation state of "published". The below code works fine until I set a node to the "published" state at which point it cannot find anything.

    $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');

    // Join table so we can check which Content Type this node is an entity of.
    $query->join('node_field_data', 'nd', 'nd.nid=nf.nid');

    $query->fields('nf', ['nid', 'vid']);

    // Compare moderation states in site workflow.
    $query->condition('ms.workflow', 'site_content');

    // Node must be in a published moderation state in site workflow.
    $query->condition('ms.moderation_state', $moderation_state);

    // Content Type of node.
    $query->condition('nd.type', $bundle_id);

    return $query->execute()->fetchAll();

How can I get this to return the latest published version of a node when the state changes to published?

This code is called by a service which is called from hook_entity_type_update().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.