I am trying to display a notification to the user in CP, if the entry is having a draft so that the next user won't redo/overdo the other draft. For that how do we find the entry has drafts?
Asked
Active
Viewed 189 times
1 Answers
1
You can use \craft\services\Drafts::getEditableDrafts to find drafts that the user can edit for the given entry:
$drafts = Craft::$app->getDrafts()->getEditableDrafts($entry);
If you want to bypass the permission check and find any drafts for the entry, regardless of permissions and current user, you can use an entry query. Use draftOf to limit the query to drafts of the given entry and status(null) to allow the query to return drafts:
$drafts = Entry::find()
->draftOf($entry)
->siteId($entry->siteId)
->status(null)
->orderBy(['dateUpdated' => SORT_DESC])
->all();
This will always return an array of drafts, which may be empty if there aren't any. If all you care about is whether a draft exists, you can execute the query with exists() instead of all(), which will just return a boolean true or false.
MoritzLost
- 11,215
- 5
- 22
notificationDurationas part of theaccessibilityDefaults. You can set that to0to show the messages indefinitely, though users can still change that in their account. That setting is only available in the latest Craft 4 version, though. I don't think there's another convenient method for this, so appending messages in JS sounds reasonable. – MoritzLost Sep 06 '22 at 07:29