cached_moderation_state
Introduction
Drupal's content moderation module uses a computed field to allow developers to access an entity's current state, but this approach has a major shortcoming: computed fields can't be used in entity queries. This module aims to provide a workaround that facilitates interoperability between entity query conditions and an entity's state.
How it works
This module provides a custom field, cached_moderation_state, that copies an entity's moderation_state field value before the entity is saved. This field is automatically added to all moderated entity bundles when the module is first installed and any time a workflow is updated, but it must be initialized for each moderated entity before first use.
For a more detailed explanation of how this module works, click here.
Installation and setup
Please consult the installation guide for more detailed instructions. Generally, the following steps are required to start using this module:
- Make the module available for installation via Composer or direct download.
- Enable the module via Drupal's module installation interface, Drush, or a
hook_post_update_NAME()implementation in a dependent module. - Initialize the
cached_moderation_statefield on all moderated entities.
After initial setup, you can use the cached_moderation_state field in entity queries.
Planned obsolescence
This module's ultimate goal is to be obsoleted by a change to Drupal core that would allow an entity's moderation state to be accessed using entity queries. We encourage everyone to help fulfill this goal by contributing to the following issue: #3025164: Provide a way for fields with computed or custom storage to decide how to impact entity queries.