2

Is it possible to output content from previous revisions of entries in the twig templates?

I need to do a score comparison between say entry 1 and entry 1.1 to see if the value has increased then output an indicator on the front end to indicate the outcome (Up arrow or down arrow most likely) kind of like a music chart style.

1

I had trouble fetching the versions entry data. Things like version.dateUpdated worked, but as soon as I tried to get the data of my custom fields, it would only return the latest data, not the one from the version.

This is what made it work: fetch the version, get the entries version using getVersionById() (docs)

{# Get versions of an entry #}
{% set lastVersions = craft.entryRevisions.getVersionsByEntryId(entry.id, entry.locale) %}

{# Get the version id and fetch the entire entry #}
{% set lastVersionId = lastVersions[0].versionId %}
{% set lastVersionEntry = craft.entryRevisions.getVersionById(lastVersionId) %}
1

Probably something very similar to what Craft does when displaying past entry revisions from a template in the control panel. :)

https://github.com/craftcms/cms/tree/master-v2/src/templates/entries/_revisions.html#L23-L36

{% if section.enableVersioning %}
    {% set versions = craft.entryRevisions.getVersionsByEntryId(entry.id, entry.locale) %}
    {% if versions %}
        <h6>{{ "Recent Versions"|t }}</h6>
        <ul class="padded">
            {% for version in versions %}
                <li><a{% if entry.getClassHandle() == 'EntryVersion' and version.versionId == versionId %} class="sel"{% endif %} href="{{ url(baseUrl~'versions/'~version.versionId) }}">
                    {{ "Version {num}"|t({ num: version.num }) }}
                    <span class="light">{{ version.dateCreated.uiTimestamp() }}, {{ version.creator }}</span>
                </a></li>
            {% endfor %}
        </ul>
    {% endif %}
{% endif %}

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.