Score:2

correct way to access node-revision change property in twig

cn flag

I have a Drupal 8.9.17 website. My theme (based on Garland) includes in templates/page.html.twig:

{% if node %}
[...]
Last updated {{ node.changed.value|format_date('custom', 'D M d Y') }}
[...]
{% endif %}

This displays the property when viewing nodes at: /node/<nid>

However, when attempting to view revisions at /node/<nid>/revisions/<vid>/view, format_date() fails with "null timestamp" error so I assume that the revision is not being properly loaded.

What is the correct way to access the property so it works at both routes?

apaderno avatar
us flag
The correct way should be showing it in the *node.html.twig* template file, which is used only for nodes.
jhnc avatar
cn flag
@apaderno is a node-revision not a node?
jhnc avatar
cn flag
@apaderno the page.html.twig has a comment that "Fully load node, if there is an automatially-loaded node" is available. Shouldn't `{% if node %}` catch if this is not the case?
apaderno avatar
us flag
The *node.html.twig* template is also used for node revisions. The question is about the *page.html.twig* template, which is the template used for every page.
Score:1
us flag

For information shown for a node, I prefer the node.html.twig template, as it doesn't require to first check the page is rendered for a node. The node.html.twig template already has the following lines.

{% if display_submitted %}
  <footer>
    {{ author_picture }}
    <div{{ author_attributes }}>
      {% trans %}Submitted by {{ author_name }} on {{ date }}{% endtrans %}
      {{ metadata }}
    </div>
  </footer>
{% endif %}

If I wanted to change the way the date is rendered, or which date is shown, without removing the other information shown in the template file, I would replace those lines with the following ones.

{% if display_submitted %}
  <footer>
    {{ author_picture }}
    <div{{ author_attributes }}>
      {% trans %}Last updated on {{ node.getChangedTime()|format_date('custom', 'D M d Y') }}{% endtrans %}
      {{ metadata }}
    </div>
  </footer>
{% endif %}

For the Garland theme, which is now a contributed theme, the part to change is the following one.

{% if display_submitted %}
  <span class="submitted">{{ date }} — {{ author_name }}</span>
{% endif %}

You can either replace those lines with the following lines, or the next ones, if you just want to change the date format.

{% if display_submitted %}
  <span class="submitted">{% trans %}Last updated on {{ node.getChangedTime()|format_date('custom', 'D M d Y') }}{% endtrans %}</span>
{% endif %}
{% if display_submitted %}
  <span class="submitted">{{ node.getChangedTime()|format_date('custom', 'D M d Y') }} — {{ author_name }}</span>
{% endif %}

As the documentation for the node.html.twig template says, not all the node properties and methods are accessible via node; only the methods whose names start with get, has, or is are available, together a few common methods such as id, label, and bundle.

jhnc avatar
cn flag
This appears to be different from the [Garland node.html.twig](https://git.drupalcode.org/project/garland/-/blob/8.x-1.x/templates/node.html.twig) which does not contain `<footer>`. I'm sure the site will eventually move to a better theme, but I'm stuck with it for now.
apaderno avatar
us flag
It's not much different: Just look at the line using `{{ date }}` and replace it with the code you need, as the updated answer shows. I was not able to find the Garland theme; I didn't think to look between the contributed themes.
jhnc avatar
cn flag
I just had a closer look at the customised garland I have been given and don't see anything that requires the code to be in page.html.twig, so using node.html.twig does seem like the way to go
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.