Создание, продвижение сайтов
40-33-54

Twig Tweak and Views

03 мая 2024

Twig Tweak's drupal_view() method provide's access to embed views within any Twig code, including dynamically from within each row of another view. This feature provides an alternate method to accomplish the nesting provided by the Views field view module.

The most basic syntax for Twig Tweak's view embed simply specifies the view and the machine name you wish to embed, as follows:

{{ drupal_view('who_s_new', 'block_1') }}
You can, however, also specify additional parameters which map to contextual filters you have configured in your view.

{{ drupal_view('who_s_new', 'block_1', arg_1, arg_2, arg_3) }}
Nested View Example
There are a lot of cases in views where you want to embed a list inside of each row. For example, when you have a list of product categories (taxonomy terms) and for each category, you want to list the 3 newest products. In this example, assume your content type is 'product', and has a reference field to a taxonomy named 'product categories'.

Step1: Create your child view
Create a view of the content of type 'product', and choose to create a block displaying an unformatted list of teasers, with 3 items per block. In this example, we'll use a view name of 'products_by_category'. Once the view is created, choose advanced and add a relationship to the taxonomy term that references 'product categories', and choose to require the relationship. Next, choose to add a "Contextual Filter" for "Term ID", and choose an action for when the filter value is not available (e.g. display contents of no results found). Set your desired sort and save your view.

Step 2: Create your parent view
Create a view of taxonomy terms of type 'product categories', and choose to create a page which displays an unformatted list of fields. Once this is created, you should see the preview showing all of the product categories. Choose to add a field of type "Term ID", and choose "Exclude from display"; this is necessary to make the term id available to the next field which uses Twig Tweak. Now, choose to add a field of type "Custom text" from the "Global" category. Inside that field, enter the Twig Tweak call to display the child view we created above, passing the tid as a contextual filter, as such:

{{ drupal_view('products_by_category', 'block_1', tid) }}
(the tid argument comes from your replacement values as {{ tid }} because of the excluded Term ID field you excluded, so if your using a diffrent argument like term name in your replacement values you will see {{ name }}, than use name as your argument.

You should now save your view, and be able to access the URL you assigned and see a list of product categories, each followed by the three most recent products within each.

This example can be applied to any nested view scenario, including multiple-levels of nesting.

Check if View has Results
{% if drupal_view_result('related', 'block_1') is not empty %}
{{ drupal_view('related', 'block_1') }}
{% endif %}

Сделайте заказ с сайта и получите 10% скидку на услугу

Заказ услуги с сайта raybin.ru

Нажимая на кнопку "Получить консультацию", я даю согласие на обработку персональных данных и соглашаюсь c условиями политики конфиденциальности
CAPTCHA
Этот вопрос задается для того, чтобы выяснить, являетесь ли Вы человеком или представляете из себя автоматическую спам-рассылку.
Заказать бесплатную SEO-консультацию

Запрос бесплатной SEO - консультации с сайта raybin.ru

Нажимая на кнопку "Получить консультацию", я даю согласие на обработку персональных данных и соглашаюсь c условиями политики конфиденциальности
CAPTCHA
Этот вопрос задается для того, чтобы выяснить, являетесь ли Вы человеком или представляете из себя автоматическую спам-рассылку.
Письмо в компанию Райбин

Письмо в компанию RAYBIN

Нажимая на кнопку "Получить консультацию", я даю согласие на обработку персональных данных и соглашаюсь c условиями политики конфиденциальности
CAPTCHA
Этот вопрос задается для того, чтобы выяснить, являетесь ли Вы человеком или представляете из себя автоматическую спам-рассылку.