2

I've built a help site for a client that pre-releases articles to a select group of beta users. The updated articles (Craft entries) are published as drafts until features make it out of beta and into the main release.

The client has gone and done the clever thing of using the Live Preview URL (with token) to share with the limited group of beta users who should only have read-only access to the article:

http://cagedwisdom.com/articles/bloopers?token=3p~rKL53nTcTrO_q1XcWsaBNla5VpzTF

Since the token is temporary, this link works for a little while and eventually expires and people can no longer review the draft content.

Is there a way to an entry's latest draft?

Let's pretend that I have a the query parameter &draft=true appended to an article URL, and I use that to selectively get the latest draft:

{% if craft.request.getParam('draft') == true %}
    {% set draftEntry = entry.drafts().order('date desc').first() %}
{% endif %}

Is there a not-imaginary way to do this?

If not, my best ideas are...

  1. To create a plugin that'd allow the editor to create a persistent but removable hash for that specific entry that'd allow access to said draft. (So if the article URL is http://cagedwisdom.com/articles/bloopers, the secret draft share URL would be http://cagedwisdom.com/articles/bloopers&draft=Ag4For3Wom8Ot5eeDd9yAf2g.)
  2. Mess with Craft's token expiration values so the shared links last longer.

1 Answer 1

4

Something like this should work:

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

{{ entry.title }}
...

This is assuming of course that the entry is published at all. If it's disabled then the uri will return 404. In which case you would have to retrieve the entry in some other way. See this answer for an example.

3
  • Brilliant! I've not used craft.entryRevisions, but that very much answers the question. Thank you.
    – Matt Stein
    Commented Jan 30, 2015 at 22:55
  • For some reason, this appears to work, but the title displayed for me in my template is of the published entry, rather than the draft. Any idea what I'm doing wrong?
    – user804
    Commented Jan 20, 2016 at 15:03
  • @danherd That's odd. Once the draft has been assigned, I would assume all fields would show the draft version. Are you getting the draft content? Or just seeing this issue with the title? Commented Jan 20, 2016 at 23:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.