2

The date is being rendered from field.html.twig template from {{ item.content }} from "items" array. After Using a twig filter date {{ items.content|date("long") }} it only outputs current date not the date node was created. How could I achieve node submitted date instead of the current date. enter image description here

5

Having a field template you always can get the parent entity via element['#object']. And from there you can get everything else, also the creation timestamp. You then can format it using Twig's date() filter or with format_date() in a predefined Drupal date format.

In any variation of field.html.twig (field--field-myfield.html.twig etc.) do:

<div class="foo">
  {{ element['#object'].getCreatedTime | date('F j, Y') }}
</div>

<div class="bar">
  {{ element['#object'].getCreatedTime | format_date('html_datetime') }}
</div>

Source: Sample taken from Accessing the original entity in field templates is hard.

2

I believe you are wanting "node.createdtime"

<p class="post-date">{{ node.createdtime | date('F j, Y') }} at {{ node.createdtime | date('h:i a') }}</p>

This is for "node" template files. No loops or custom fields necessary as this comes from Drupal core.

{{ dump(node | keys) }}
  • Ok. I guess if the "node submitted" info is coming from a field it will probably depend on the field type... I would look to see if you have display settings control. Razeem Ahmed's answer is correct. date() in twig can't format "Sun, 01/11/2010". So whether in display settings, or in preprocessing, you'll need to ensure it renders as a timestamp. – Prestosaurus Mar 28 '18 at 14:13
  • You were right I was able to control the custom date I created from drupal UI and implemented on custom DS view mode, however, I was not able to manipulate node created date implementing preprocessor. $date = $variables['node']->getCreatedTime(); $variables['date'] = \Drupal::service('date.formatter')->format($date, 'my_custom_date_formate_machine_name'); this is the code i am trying to implement on my preprocessor for that field but something is not right, where did i went wrong? – dejavu Mar 28 '18 at 22:02
0

In twig, date filter only works for timestamps. In above case it is assuming as the present date and printing the date in long format.

You can get the created timestamp by writing a preprocess.

$timestamp = $variables['node']->created->value
  • 1
    how can i achieve my desired output then? – dejavu Mar 27 '18 at 9:54
  • I cannot recreate the issue here. You need to access the arguments @date. The best way is to preprocess the data in you hook_preprocess() and then print the variable – Razeem Ahmad Mar 27 '18 at 9:57
  • @dejavu if you are writing a pre-process then you can access the created timestamp by $variables['node']->created->value – Razeem Ahmad Mar 29 '18 at 10:34
  • 1
    You can combine the date and format_date filters to get the timestamp and then format it with a predefined Drupal date format: {{ node.field_my_date.value|date('U')|format_date('html_datetime') }} – leymannx Jan 7 at 21:42
0

If you are in views and want to print node created datetime value in twig you need to:

1. If your views HAS outputted Field (Authored On): then {{ fields.created.content }}

2. If NOT: then {{ row._entity.created.value }}

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.