When editing an entry from the control panel you can switch versions of the entry. I would like to do the same thing but on the front end. Is there any way to do this using native craft functionality or do I need to use a plugin.
1 Answer
Something like this might work.
{% if not craft.request.isAjax %}
{% set versions = craft.entryRevisions.getVersionsByEntryId(entry.id, entry.locale) %}
Versions:
<select class="js-versions-dropdown">
<option value="">Current</option>
{% for versionEntry in versions %}
<option value="{{ versionEntry.versionId }}">Version {{ loop.index }} ({{ versionEntry.postDate|date('m-d-Y') }} )</option>
{% endfor %}
</select>
{% endif %}
{% if craft.request.isAjax %}
{% if craft.request.param('versionid') %}
{% set entry = craft.entryRevisions.getVersionById(craft.request.param('versionid')) %}
{% endif %}
{% endif %}
<div>
{{ entry.title }}
...
</div>
In this example, you would want to listen for an onChange
event on the dropdown list and reload the template via ajax, passing the 'versionid' as a post variable. Alternatively you could also just reload the whole page, in which case remove the 'if ajax' stuff. In this case, you'll probably also need to add some logic to the drop-down list so as to display the correct 'selected' option in the list.
-
This is perfect. Thank you! Is there any way to just list each versions contents without ajax? I tried using
{{versionEntry.fieldName}}
in the for loop but it just output the same value as the current version even though the field has different values for different versions. Commented Apr 6, 2015 at 18:07 -
Should work fine without ajax — as a test I was able to use {{ versionEntry.title }} in the for loop and display the different version titles just fine. Not sure why your not seeing the different content — you might double check the saved version content. Commented Apr 7, 2015 at 0:27