Skip to content

Store entry statuses statically #17024

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 4, 2025

Conversation

brandonkelly
Copy link
Member

Description

Adds a new status column to the entries table, which statically stores entries’ last-known date-based status (live, pending, or expired), updated whenever entries are saved.

By default the column is completely ignored by element queries, and entry queries will continue taking the postDate and expiryDate columns into account when fetching entries by live/pending/expired status.

However, that behavior can be altered using a new staticStatuses config setting. When that’s enabled, entry queries will look at the status column instead of postDate and expiryDate. So if an entry is saved with a future post date, and its status column is set to pending, it will continue to be excluded from entry query results for live entries, even passed its post date, until it’s saved again.

There’s also a new update-statuses command, which will look for entries whose statically-stored statuses need to be updated based on their postDate and expiryDate values, and resaves them so their statuses get updated (and caches get cleared, etc.).

Sites with staticStatuses enabled should start running update-statuses every 1 or 5 minutes using a Cron job, to ensure that statically-stored entry statuses are kept up-to-date.

And finally, entries have a new oldStatus property, making it easy for event listeners to catch the moment that an entry goes live or expires:

use craft\base\Event;
use craft\elements\Entry;
use craft\events\ModelEvent;
use craft\helpers\ElementHelper;

Event::on(Entry::class, Entry::EVENT_AFTER_SAVE, function (ModelEvent $event) {
    /** @var Entry $entry */
    $entry = $event->sender;

    if (
        !ElementHelper::isDraftOrRevision($entry) &&
        $entry->getStatus() !== $entry->oldStatus
    ) {
        if ($entry->getStatus() === Entry::STATUS_LIVE) {
            // the entry just went live
        } elseif ($entry->oldStatus === Entry::STATUS_LIVE) {
            // the entry just stopped being live
        }
    }
});

Related issues

Copy link

linear bot commented Apr 4, 2025

@brandonkelly brandonkelly merged commit d5b601a into 5.7 Apr 4, 2025
@brandonkelly brandonkelly deleted the feature/cms-74-statically-stored-entry-statuses branch April 4, 2025 15:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant