4

I've been searching for a method to load in an existing entries drafts. Here is what I am trying to implement.

User can add or edit an entry from the front end of my craft site. They cannot publish. Admin can see new drafts and publish them.

I see how to submit a draft update via the front end using the entryRevisions/saveDraft method. But how can I load in the current entries draft information into a form on the front end for a user to edit? Hopefully I missed this, or is it undocumented.

  • possible duplicate of Does Craft Have Publishing Workflow? – carlcs Jan 22 '15 at 16:33
  • Not a duplicate. That post has absolutely no reference to the draft system or the method I'm looking for. – etx Jan 22 '15 at 16:47
  • 1
    Yep right, thought you meant save without publish. Sorry! – carlcs Jan 22 '15 at 17:47
5

After doing some digging around I found the getDraftsByEntryId(entryId, locale) method of the craft.entryRevisions service that might help.

The following should return the first draft of an entry. Note: there is a second argument for 'locale', which defaults to current locale if none provided. Also not sure whether 'first' or 'last' would be considered the most recent draft.

{% set drafts = craft.entryRevisions.getDraftsByEntryId(entry.id) %}
{% if drafts|length %}
    {% set draft = drafts[0] %}
    {{ draft.title }}
    ...
{% endif %}

You will need to load the entry manually (i.e. through a custom route of some kind) as the draft's entry is likely to be 'disabled' (although not necessarily) and thus not load through the entry's default url. Something like '/mysection/edit/slug' perhaps (where 'slug' is a custom route token).

{% set entry = craft.entries({
    section: 'mysection',
    slug: slug
    status: null
}).first() %}
  • Works like a charm! Not sure how I missed that method, but thanks for the help. – etx Jan 23 '15 at 15:16

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.