0

I'm currently working on Bartik theme and i'm trying to create a custom template for the Content-Recent block available in Drupal 8 For your information, i would like to create the following block view :

enter image description here

For that, I've followed these steps :

  • In Drupal 8 back-office, I've added the block view "Recent content" (Machine name : views_block__content_recent_block_1)
  • In my source code, I've created a new file in my templates folder and named : views-view-list--content-recent--block-1.html
  • I've added a static text : "Hello world", just to check if the template has been override, and it's OK for this part.

But now, I'm stuck because I don't know how to retrieve item properties (Title, Date, Description) I've tried to find some informations with kint() and dump() but I'm bit lost :/

0

This is the default template for a views-view-list.html.twig. Basically you've overwritten this template when you've created views-view-list--content-recent--block-1.html.

{#
/**
 * @file
 * Default theme implementation for a view template to display a list of rows.
 *
 * Available variables:
 * - attributes: HTML attributes for the container.
 * - rows: A list of rows for this list.
 *   - attributes: The row's HTML attributes.
 *   - content: The row's contents.
 * - title: The title of this group of rows. May be empty.
 * - list: @todo.
 *   - type: Starting tag will be either a ul or ol.
 *   - attributes: HTML attributes for the list element.
 *
 * @see template_preprocess_views_view_list()
 *
 * @ingroup themeable
 */
#}
{% if attributes -%}
<div{{ attributes }}>
    {% endif %}
    {% if title %}
        <h3>{{ title }}</h3>
    {% endif %}

    <{{ list.type }}{{ list.attributes }}>

    {% for row in rows %}
        <li{{ row.attributes }}>{{ row.content }}</li>
    {% endfor %}

</{{ list.type }}>

{% if attributes -%}
    </div>
{% endif %}

What you could do is try adding something like this inside your "for" loop:

{% set item = row.content['#view'].style_plugin.render_tokens[ loop.index0 ] %}
{{ item['{{ title }}'] }}
{{ item['{{ field_date }}'] }}
  • Finally by digging my variables with kint() and following your solution, I'm able to get the data that I want to display. For example : To get the bundle name associated to my article : row.content['#row]._entity.bundle()` – Codaline May 23 at 12:59

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.