The Publication date module adds a new date field that is unpopulated until you click publish. It is easy to install and can be fully configured in the Drupal UI. Let's take a look.
The module can be installed like any other module.
Display the Publication Date in your Template
Once installed, you need to check the manage display page for each content type. Below is the path for the article content type.
Manage > Structure > Content types > Article > Manage display
/admin/structure/types/manage/article/display
On the manage display page, drag the new Published on field to the position you would like it to appear.
The new field will appear where the {{ content }}
tag is in your theme template.
Format the display of the field and the date
Once the field is enabled, you can hide the field label and change the format.
You can leave it as the default format or use a time ago format.
For the default format, you can change the date format as shown in the below image.
You can use any of the date formats set up in the system or add a custom format using PHP date and time formats.
To add a new custom date format to use site-wide, you can do that on the date and time formats page.
Manage > Configuration > Region and language > Date and time formats
/admin/config/regional/date-time
Using the Publication Date module field in your custom theme
If you have a custom theme or a customized template, you may need to add the new field to your template where you want it to be rendered. You can do that using the below twig variable.
{{ content.published_at }}
You may also want to remove it from the content variable.
{{ content|without('published_at') }}
I have written about how you can add the revision or updated date to your articles. If you use this module, you will want to replace the {{ date }}
variable with the new {{ content.published_at }}
variable.
However, be aware that it will not render inside the {% trans %}
tags, which is discussed in the article. Placing it outside of the translation tags is the easiest solution.
Summary
That's it for the Publication date module. We have looked at how easy it is to install and use on a basic site all within the Drupal UI.
Then we looked at how you can add the new field to your custom theme. If you were keen, you would have also learnt how you can add the revision date to your articles.
I hope this was useful for you. I do feel this should be included with all sites. It makes the content creation and publishing workflow much simpler for your users.
If you are interested in leveraging Drupal as your CMS and front-end development and design, sign up for my newsletter at the bottom of the page. Thanks for reading, and until next time, seize the day.