2

Is there an easy way to fetch the selected Site, Revision and Draft of the dropdown (the one an editor would use to switch the Site/Revision/Draft) in control panel in a plugin?

The only way I found so far is to get it from the provided URL parameters. This works but I don't feel very safe using it and it won't work in any CLI script, because of the missing Request.

1

In the context of the Control Panel, the URL parameters are the best way to get that info because the user chooses which site to work on, which draft to work on, or which revision to review. Craft, itself, relies on those URL parameters.

In the context of a CLI request, there is no user interaction. You're on the hook to sort out which entry, draft, site, and user are relevant to the element you'd like to manipulate. As of Craft 3.3 (current version today) there is an EntryRevisions service class. All of the methods are deprecated in favor of running your own entry queries, but those methods are a good reference for how to get the drafts or revisions you're looking for.

For example, rather than running the deprecated method:

$drafts = Craft::$app->getEntriesRevisions()->getDraftsByEntryId($entryId, $siteId);

You'd run the contents of that method for yourself:

return Entry::find()
    ->draftOf($entryId)
    ->siteId($siteId)
    ->orderBy(['drafts.name' => SORT_ASC])
    ->all();
| improve this answer | |

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.