Score:0

How to access data in Twig in module extended template

us flag

I'm new at developing Drupal. I'm using Drupal 8 and I want to display datetime fields in a special way.

Here is how my module_name.module and template file looks like:

function module_name_theme($existing, $type, $theme, $path) {
  $variables = [
    'field__node__field_course_date__course' => [
      'template' => 'field_node_field-course-date_course',
    ],
  ];

  return $variables;
}

field_node_field-course-date_course.html.twig:

Some text to display

Now I'd like to display dates but I'm not sure how to access them. I see there is a _context.element.items variable but it's empty. What is the proper way to access datetime fields data in template extended from module?

ru flag
It is usually not a good idea to do this in a module, better use a theme and one of the pre-existing template suggestions.
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.