5

How can I display the current moderation state for the node in a view and filter on it?

1 Answer 1

5

In Drupal 8.5 this has gotten a lot easier.

Pre-requesities:

1) Enable content_moderation (which enables workflows).

2) Set up a workflow with content moderation states.

3) Create a new view with the base table "Content Revisions". This is important, as the main content out of the box is based on "Content" which refers to the default revision. We want to see the latest revision. I made this mistake at first where I took the default content view and added a relationship to Content: Moderation State. This is the moderation state of the default revision, which is not what we want to see. We want to see the moderation state of the latest revision.

4a) Under "Filter Criteria" add filter "Is Latest Revision".

enter image description here

5) To display the moderation state, under "Fields" select "Content Revision: Moderation state".

enter image description here

6) To filter by moderation state, under "Filter Criteria" add filter "Content Revision: Moderation state" and expose it.

7) Save your view.

8) You'll need to update your content so that it has moderation states. This can be done by resaving it.

For example you could run:

  $article_nids = \Drupal::entityQuery('node')
    ->condition('type', 'article')
    ->execute();

  $articles = \Drupal::entityTypeManager()->getStorage('node')->loadMultiple($article_nids);
  foreach ($articles as $article) {
    $article->save();
  }

Now your view should show moderation states.

It looks like Drupal 8.5 also adds a subtask on the system.admin_content route that shows a link to a page that displays moderated content.

If you'd like to turn off these subtasks:

/**
 * Implements hook_menu_local_tasks_alter().
 */
function mymodule_menu_local_tasks_alter(&$data, $route_name) {
  if ($route_name == 'system.admin_content') {
    if (!empty($data['tabs'][1]['content_moderation.content'])) {
      unset($data['tabs'][1]['content_moderation.content']);
    }
    if (!empty($data['tabs'][1]['content_moderation.moderated_content'])) {
      unset($data['tabs'][1]['content_moderation.moderated_content']);
    }
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.