Adding Updated Date to Your Drupal Articles

Published Sep 8, 2023
Updated Sep 8, 2023
By Simon
Table of contents

Having a published date is obviously an important piece of information to have on any article and Drupal adds this out of the box. But in a lot of cases having an updated date is also a good idea. This article is going to look at how we can add an updated date using a line of code in your node template and for those hungry for a little more we'll look at how to add the revision comment and a few other things to think about. 

Publish date is a not created date. 
One small thing I have always found strange about Drupal is the publish date is not saved. You either need to clear the created date and set it to the current date and time manually or you can use the Publication date module

Adding the article changed time or revision time to your content.

After a bit of research digging through documentation and the twig dump, I have found you have a few options to use here. 

Using the last saved date time from the node edit form

The first option is to add the last saved date of your article. To do this we can use the below code. 

{{ node.changedtime|format_date('custom', 'M j, Y - H:i') }}

One thing to note is that this will not work if you add inside the trans tag. So you can't just replace the {{ date }} value but you need to place it outside the trans tags. For some reason, the format date filter doesn't work inside the tag.  
You could set a variable like below if you like. However, I haven't tested this with a language that is translated from the default English. 

{% set changedtime_formatted = node.changedtime|format_date('custom', 'M j, Y - H:i') %}
{% trans %}
  Submitted by {{ node.uid.entity.field_profiel_firstname[0].value }} {{ author_name }} on {{ date }} Updated: {{ changedtime_formatted  }}
{% endtrans %}

So this is great now every time you save your piece of awesome content the change date is added. Double awesome. 

After a little more digger, I have found a great alternative though. And for a lot of cases maybe this will be a better solution. The alternative to the changed date is the revision date. 

Using a current revision date with the author name on node content

What is the revision date?

Drupal has a great built-in functionality for tracking revisions. This can be set as always being added or as optional. For my site, I like to have it as optional so that I can make small saves without adding a new revision. Small saves maybe spelling mistakes and so forth. I won't go into the pros and cons of having this enabled or disabled by default as on a big site with a lot of users you may want to track all diffs.

That said, if you have it set to create revisions on every save then the revision date will be the same as the change date.

With the not so little pre-amble out of the way let's take a look at the code. 

{{ node.RevisionCreationTime|format_date('long') }}

That's it, now you can render the latest revision date in place of the change time.

It is also possible to add the revision comment as well. 

The below code will give you the revision log message which could be useful to let people know what was updated.

{{ node.RevisionLogMessage }} 

A more complex publishing workflow

A little bit of a rabbit hole but I am going to add some ideas here for people with more complex publishing workflows since I thought about it. 

To only render a revision date of importance, say new concepts or better explanations, you could use this log message field and populate it with a variable you could filter on or only show only if a log message exists. You would also need to use views or a custom theme function to display an older revision in case the latest one didn't have a message. This would mean having a strict manual publish workflow in place, however. 

Another idea. If you wanted a more fail-proof solution, you could modify the revision section with a use this revision date check box and then use this as your control flow. In fact, the options are endless so I will leave this here. 

Work is being done on making revisions comments mandatory if you want to go this way so some code is already available to get you kick-started. Here is the issue on Drupal, Allow revision log messages to be required. 

As you can see things can start to get complex quickly if you allow them. I do feel the solution I use is solid. That is to have create new revision & log message disabled by default and having to explicitly check it to add it. If you need help in setting up a site please get in touch and I'd be happy to help.

Summary

Well, that's it for adding an updated date to your content. I hope you have learnt something. Twig is such a fun language to use for front-end templating in PHP and Drupal, it makes it a pleasure to build themes. To find out more about Twig you can check the Twig search results on my site.

Thanks for reading and be sure to sign up for my newsletter. I write about front-end development and design and I am passionate about making beautiful and accessible UI/UX.

Bye, for now, mate ne. (まてね see you again)

 

Tags