Answers.Body: <p>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.</p>
  
  <p>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 <a href="https://github.com/craftcms/cms/blob/develop/src/services/EntryRevisions.php" rel="nofollow noreferrer">EntryRevisions service class</a>. 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.</p>
  
  <p>For example, rather than running the deprecated method:</p>
  
  <pre class="lang-php prettyprint-override"><code>$drafts = Craft::$app-&gt;getEntriesRevisions()-&gt;getDraftsByEntryId($entryId, $siteId);
  </code></pre>
  
  <p>You'd run the contents of that method for yourself:</p>
  
  <pre class="lang-php prettyprint-override"><code>return Entry::find()
      -&gt;draftOf($entryId)
      -&gt;siteId($siteId)
      -&gt;orderBy(['drafts.name' =&gt; SORT_ASC])
      -&gt;all();
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-10-24T18:18:29.967Z
Answers.LastActivityDate: 2019-10-24T18:18:29.967Z
Answers.UserDisplayName: Sam Hernandez
Body: <p>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? </p>
  
  <p>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.</p>
  
Tags: <plugin-development><craft3><control-panel>
Title: How do I get the Site/Revision/Draft in a field in the control panel?
ThreadText: # How do I get the Site/Revision/Draft in a field in the control panel?
  
  (Asked by: username_0 on 2019-07-24)
  
  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.
  
  **Tags:** plugin-development, craft3, control-panel
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2019-10-24. Score: 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](https://github.com/craftcms/cms/blob/develop/src/services/EntryRevisions.php). 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:
  
  ```php
  $drafts = Craft::$app->getEntriesRevisions()->getDraftsByEntryId($entryId, $siteId);
  
  ```
  
  You'd run the contents of that method for yourself:
  
  ```php
  return Entry::find()
      ->draftOf($entryId)
      ->siteId($siteId)
      ->orderBy(['drafts.name' => SORT_ASC])
      ->all();
  
  ```
  
  
  ---
  
  