<p>You can use <a href="https://%20getEditableDrafts" rel="nofollow noreferrer"><code>\craft\services\Drafts::getEditableDrafts</code></a> to find drafts that the user can edit for the given entry:</p>
<pre class="lang-php prettyprint-override"><code>$drafts = Craft::$app-&gt;getDrafts()-&gt;getEditableDrafts($entry);
</code></pre>
<p>If you want to bypass the permission check and find <em>any</em> drafts for the entry, regardless of permissions and current user, you can use an entry query. Use <a href="https://craftcms.com/docs/4.x/entries.html#draftof" rel="nofollow noreferrer"><code>draftOf</code></a> to limit the query to drafts of the given entry and <a href="https://craftcms.com/docs/4.x/entries.html#status" rel="nofollow noreferrer"><code>status(null)</code></a> to allow the query to return drafts:</p>
<pre class="lang-php prettyprint-override"><code>$drafts = Entry::find()
    -&gt;draftOf($entry)
    -&gt;siteId($entry-&gt;siteId)
    -&gt;status(null)
    -&gt;orderBy(['dateUpdated' =&gt; SORT_DESC])
    -&gt;all();
</code></pre>
<p>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 <code>exists()</code> instead of <code>all()</code>, which will just return a boolean <code>true</code> or <code>false</code>.</p>

CC BY-SA 4.0
2022-09-05T03:13:11.053Z
Thanks for the details in addition to the above, could you point me to how to display an html notification on the control panel?
Parthiban
CC BY-SA 4.0
2022-09-05T07:43:04.757Z
@Parthiban Craft refers to those notifications as _flash messages_, you can set them through the session component. See [here](https://craftcms.stackexchange.com/questions/29094/how-do-you-set-a-flash-message-in-the-template-in-craft-3) and [here](https://craftquest.io/lessons/using-flash-messages-in-craft-and-twig) for details.
MoritzLost
CC BY-SA 4.0
2022-09-05T14:28:31.233Z
Thanks, @moritzlost But the above solutions won't display permanently(after certain seconds it will auto-hide). I was looking to show a notification permanently. Currently, I append HTML elements with JS. Let me know if there are any specific notification methods we can use.
Parthiban
CC BY-SA 4.0
2022-09-06T07:29:27.023Z
@Parthiban In Craft 4 there's a new setting for the default duration that flash messages are visible for, you can set the `notificationDuration` as part of the `accessibilityDefaults`. You can set that to `0` to 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
CC BY-SA 4.0
2022-09-06T09:48:37.637Z
Thanks for the details @moritzlost
Parthiban
CC BY-SA 4.0
2022-09-02T11:19:53.073Z
2022-09-02T11:19:53.073Z
MoritzLost
<p>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?</p>

<craft3><control-panel>
How to find if there is a draft for a particular entry in control panel plugin
# How to find if there is a draft for a particular entry in control panel plugin

(Asked by: username_0 on 2022-09-02)

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?

**Tags:** craft3, control-panel

---

## 1 answers

### Answer by username_1 on 2022-09-02. Score: 1 | Accepted answer

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.

> **Comments:**
> **username_0**: Thanks for the details in addition to the above, could you point me to how to display an html notification on the control panel?  
> **username_1**: @Parthiban Craft refers to those notifications as _flash messages_, you can set them through the session component. See [here](https://craftcms.stackexchange.com/questions/29094/how-do-you-set-a-flash-message-in-the-template-in-craft-3) and [here](https://craftquest.io/lessons/using-flash-messages-in-craft-and-twig) for details.  
> **username_0**: Thanks, @moritzlost But the above solutions won't display permanently(after certain seconds it will auto-hide). I was looking to show a notification permanently. Currently, I append HTML elements with JS. Let me know if there are any specific notification methods we can use.  
> **username_1**: @Parthiban In Craft 4 there's a new setting for the default duration that flash messages are visible for, you can set the `notificationDuration` as part of the `accessibilityDefaults`. You can set that to `0` to 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.  
> **username_0**: Thanks for the details @moritzlost  

---

