1

Is it possible to allow selecting drafts in an entries field? Not drafts of an existing live entry, but drafts that aren't published yet.

The only thing I found is an ElementCriteriaEvent triggered in the BaseRelationField class which allows you to add selection criteria for the field, but I couldn't figure out a way to change the allowable statuses using the criteria (by the way, is the syntax for those criteria arrays documented anywhere?).

Here's my attempt which doesn't work:

use craft\events\ElementCriteriaEvent;
use craft\fields\Entries;
use yii\base\Event;

Event::on(
    Entries::class,
    Entries::EVENT_DEFINE_SELECTION_CRITERIA,
    function (ElementCriteriaEvent $e) {
        $e->criteria = ['status' => ['live', 'draft']];
    }
);
2

drafts is not a status but an Element Query parameter so if you want drafts to be return as well as live entries, you can do:

Event::on(
    \craft\fields\Entries::class,
    \craft\fields\Entries::EVENT_DEFINE_SELECTION_CRITERIA,
    function (\craft\events\ElementCriteriaEvent $e) {
        $e->criteria = ['drafts'=> null];
    }
);

Passing true would only return Drafts, false (default) no Drafts and null takes it out of the query.

1
  • Thanks, works perfectly! Unfortunately, while drafts now show up for selection, they're thrown out upon saving because the BaseRelationField::normalizeValue function doesn't allow drafts. I've opened an issue on Github about this – hopefully that leads somewhere, as it stands the only way to allow selecting drafts would be to extend the entries field and to modify the normalizeValue method to allow drafts.
    – MoritzLost
    Oct 14 at 8:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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