2

We have some content that needs to display a log of changes (legal docs, terms etc)

Could I use version history for this?

I found this for Craft 2 but doesn't work on 3...

How can I display entry edit history on the front end?

3

Here's an example of accessing an entry's version history that Nathaniel Hammond recently shared on Twitter:

{#
Options for getVersionsByEntryId
* @param int $entryId The entry ID to search for.
* @param int|null $siteId The site ID to search for.
* @param int|null $1 limit The limit on the number of versions to retrieve.
* @param bool $includeCurrent Whether to include the current "top" version of the entry.
* @param bool $withContent Whether the field content should be included on the versions.
* @return EntryVersion[]
#}
{% set entryId = 7 %}
{% set siteld = null %}
{% set limit = null %}
{% set includeCurrent = true %}
{% set withContent = false %}
{% set versions = craft.app.entryRevisions.getVersionsByEntryId(entryId, siteId, limit, includeCurrent, withContent) ?? null %}

{% if versions|length %}
  <ul>
    {% for version in versions if version.getCreator() %}
      <li>{{ version.getCreator().username }}</li>
    {% endfor %}
  </ul>
{% endif %}

In this example he's accessing the version's creator and displaying that, but you could easily swap that out for whatever you need to do, such as grabbing the version creation date etc.

  • Incredible thank you! – alex morris May 29 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.