1

How do I get the last editor of an entry on the front end? There are answers to this question here, but they relate to plug-ins. I'd like to access the information directly.

{{ entry.author.username }} returns the original author, but I need the person who most recently edited the entry.

4midori
  • 656
  • 6
  • 22

1 Answers1

3

There is no "direct" attribute (i.e. similar to entry.author) for getting the user who last edited an entry. This is because entries don't actually keep track of their "history"; this is the responsibility of the revision system.

Essentially, what you're looking for is the creator of the current (i.e. last/newest) entry revision.

Unfortunately, Craft doesn't make it very easy to get to that revision creator user - but it can be achieved using a custom database query.

There are a few different ways this query can be approached, but perhaps one of the most effective ones is by creating a UserQuery (since we're looking for a user!), and using an inner join on the revisions database table to conditionally query for the same user that is referenced in the current revision's creatorId database column:

{% set revisionCreatorUser = craft.users
    .innerJoin('{{%revisions}} AS revisions', "revisions.creatorId = elements.id AND revisions.id = #{entry.currentRevision.revisionId}")
    .one()
%}

It's probably a good idea to code this a bit more defensively though, and to make the code fall back to the entry.author in cases where there is no current revision, or the revision creator can't be found:

{% set currentRevisionId = entry.currentRevision.revisionId|default %}
{% if currentRevisionId %}
    {# Query for the current revision's creator #}
    {% set revisionCreatorUser = craft.users
        .innerJoin('{{%revisions}} AS revisions', "revisions.creatorId = elements.id AND revisions.id = #{currentRevisionId}")
        .one()
    %}
{% endif %}

{# Set the lastEditedByUser variable to the revision creator if it exists, or the entry author if not #} {% set lastEditedByUser = revisionCreatorUser ?? entry.author %}

<p>Entry last edited by: {{ lastEditedByUser.username }}</p>

Mats Mikkel Rummelhoff
  • 22,361
  • 3
  • 38
  • 69