Integrating content moderation for previewsfor Drupal 8 , 9 , 10 , and 11

Last updated :  

In the previous lesson, we explored setting up preview routes for draft content in a headless Drupal environment. In this lesson, you'll learn how to integrate content moderation processes to effectively manage and review content before it is published.

The Role of Content Moderation

Content moderation workflows are vital for editorial teams to ensure that content passes through necessary reviews and approvals before going live. In a headless Drupal setup, integrating moderation involves configuring your Drupal site to transition content across different workflow states such as Draft, Review, and Published, and updating how this integrates with your preview configuration.

Enabling Content Moderation in Drupal

To leverage content moderation capabilities in Drupal, begin by enabling the Content Moderation module, which is included in Drupal core. It provides a flexible system to create moderation workflows tailored to your organization’s needs.

Step 1: Enable Content Moderation Module

Enable the Content Moderation module from the Extend page or use Drush:

drush en content_moderation

Once enabled, you can access the moderation workflows from the admin UI under Configuration > Content Authoring > Workflows.

Step 2: Configure a Moderation Workflow

Define a moderation workflow that fits your editorial process. A typical workflow might include states such as Draft, Needs Review, and Published.

// Sample Workflow Configurations

1. Go to Configuration > Workflows > Add Workflow.
2. Name your workflow (e.g., "Content Approval Workflow").
3. Define states: Draft, Needs Review, Published.
4. Set transitions: Draft to Needs Review, Needs Review to Published, etc.
5. Assign workflow to content types, such as Articles or Pages.

Integrating Moderation in API Responses

By incorporating content moderation, you need to ensure that your headless setup reflects these workflow states, especially for content previews.

JSON:API Integration

For JSON:API endpoints, you'll ensure that queries return data based on user permissions and workflow states. Typically, the endpoint will include the "status" field that corresponds to the moderation state.

Example Request:

GET /jsonapi/node/article?filter[status][value]=draft&filter[uid.id][value]=[user-id]
Authorization: Bearer [token]

GraphQL Integration

If you are using GraphQL, the approach would include querying nodes with specific moderation states.

query {
  nodeQuery(filter: {conditions: [{field: "status", value: "draft"}]}) {
    entities {
      ... on NodeArticle {
        title
      }
    }
  }
}

Setting Up Previews with Moderation

Customize your frontend applications to handle different moderation states appropriately. This typically involves adding conditional logic to your API integration to handle content in these states:


// Example in a React/Next.js app
if (content.status === 'Draft') {
  // Enable preview mode
  enablePreview(content);
}

Ensuring Secure Access

Previews and moderation require strict access controls to ensure that incomplete content does not reach unintended audiences. Implement authentication mechanisms like OAuth2 or token-based authentication for secure API access.

Conclusion

Integrating content moderation for previews in a headless Drupal setup enhances your content workflow by providing a structured path for content creation, review, and approval. It ensures that only finalized content reaches your audience, maintaining quality and consistency.

In the next lesson, we'll delve into allowing frontend applications to preview unpublished content effectively, continuing to build on these core concepts. Stay tuned!