1

I have Article content type.

Moderation is enabled.

So user will move article from

Draft -> Published -> Draft -> Draft -> Published

.

So all revisions is going to create for each state.

  $entity = $form_state->getFormObject()->getEntity();
  $node_revision_ids = \Drupal::entityTypeManager()->getStorage('node')->revisionIds($entity);

  if (count($node_revision_ids) > 0) {
    $published = $updated = [];
    foreach ($node_revision_ids as $vid) {
      $node_version = node_revision_load($vid);
      if ($node_version->field_moderation_state->value == 'published') {
        $published[$vid] = $node_version->created->value;
        $updated[$vid] = $node_version->changed->value;
      }
    }
  }

  $published_first = reset($published); // Get first published date.
  $updated_last = end($updated); // Get last updated date.

So I'm loading all revision of node & on basis of status I'm checking.

Is there a better way to do this?

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.