0

How to display since when the node was created with TWIG. Here is the templace of my node :

  <div class="card mb-5 p-0 overflow-hidden shadow rounded bg-white">
    <div class="card-header position-relative">
      {{ content.group_content }}
    </div>
    <div class="card-body">
      <h1 class="p-0 mt-0">{{ node.label }}</h1>
      <div class="col-xs-3 col-sm-3 col-md-2 col-lg-2 bs-left pl-0 mb-3">
        {{ date }}
      </div>
      <div class="col-xs-9 col-sm-9 col-md-10 col-lg-10 bs-right pr-0 mb-3">
        <div class="mb-4">
          {{ content.field_discussion_description }}
        </div>
        {{ content.field_discussion_image }}
      </div>
    </div>
    <div class="card-footer d-flex flex-wrap justify-content-start pt-0">
      <div class="mt-3 mr-3">
        {{ content.flag_report_node }}
      </div>
      <div class="mt-3 mr-3">
        {{ content.flag_like_node }}
      </div>
      <div class="mt-3 mr-3">
        {{ drupal_block('shariff_block') }}
      </div>
    </div>
  </div>

I managed to do this for comments with the following code :

/**
 * Comment date.
 */
function bootstrap_subtheme_front_office_old_preprocess_comment(&$variables) {
  // Getting the node creation time stamp from the comment object.
  $date = $variables['comment']->getCreatedTime();
  // Here you can use drupal's format_date() function, or some custom php date formatting.
  $variables['created'] = \Drupal::service('date.formatter')->formatInterval(REQUEST_TIME - $date);
  $variables['submitted'] = t('@username commented !datetime', array('@username' => $variables['author'], '!datetime' => '<span class="comments-ago">' . $variables['created'] . ' ago </span>'));
}

I start doing the same thing with the knots but I do not know what I have to put as code :

/**
 * Node date.
 */
function bootstrap_subtheme_front_office_old_preprocess_node(&$variables) {
  $date = $variables['node']->getCreatedTime();
  $variables['created'] = \Drupal::service('date.formatter')->formatInterval(REQUEST_TIME - $date);
  $variables['submitted'] = t('@username commented !datetime', array('@username' => $variables['author'], '!datetime' => '<span class="comments-ago">' . $variables['created'] . ' ago </span>'));
}
  • You have to repeat this for mytheme_preprocess_node. You are outputting date in the twig template which is undefined, and not using either created or submitted. – Kevin Dec 4 '19 at 19:16
  • @leymannx Thank you but I would like to know the method with the theme – a713f93eb1 Dec 4 '19 at 19:43
  • @Kevin I do not understand what you mean. Have to modify my TWIG file or my .theme file. – a713f93eb1 Dec 4 '19 at 19:46
0

You can define a new date format in my case 'dd_mm_yyyy' (id of my date format) after that

 function bootstrap_subtheme_front_office_old_preprocess_node(&$variables) {
   $date_formatter = \Drupal::service('date.formatter');
   $node = $variables['node'];
   $variables['created'] = ($date_formatter->format($node->getCreatedTime(), 
  'dd_mm_yyyy'));}

finely in your twig file

 <div class="col-xs-3 col-sm-3 col-md-2 col-lg-2 bs-left pl-0 mb-3">
    {{ created }}
  </div>

Not the answer you're looking for? Browse other questions tagged or ask your own question.