Browse Source

Clean-up views.md

3.1.x
Chi 4 years ago
parent
commit
2345de8977
  1. 29
      docs/views.md

29
docs/views.md

@ -3,30 +3,28 @@
Twig Tweak's `drupal_view()` method provides access to embed views within any Twig Tweak's `drupal_view()` method provides access to embed views within any
Twig code, including dynamically from within each row of another view. This Twig code, including dynamically from within each row of another view. This
feature provides an alternate method to accomplish the nesting provided by the feature provides an alternate method to accomplish the nesting provided by the
Views field view module. [Views Field View](https://www.drupal.org/project/views_field_view) module.
The most basic syntax for Twig Tweak's view embed simply specifies the view and The most basic syntax for Twig Tweak's view embed simply specifies the view, and
the machine name you wish to embed, as follows: the machine name you wish to embed, as follows:
```twig ```twig
{{ drupal_view('who_s_new', 'block_1') }} {{ drupal_view('who_s_new', 'block_1') }}
``` ```
You can, however, also specify additional parameters which map to contextual You can, however, also specify additional parameters which map to contextual
filters you have configured in your view. filters you have configured in your view.
```twig ```twig
{{ drupal_view('who_s_new', 'block_1', arg_1, arg_2, arg_3) }} {{ drupal_view('who_s_new', 'block_1', arg_1, arg_2, arg_3) }}
``` ```
Nested View Example ## Nested View Example
There are a lot of cases in views where you want to embed a list inside of each There are a lot of cases in views where you want to embed a list inside each
row. For example, when you have a list of product categories (taxonomy terms) 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, 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 assume your content type is 'product', and has a reference field to a taxonomy
named 'product categories'. named 'product categories'.
Step1: Create your child view ### Step1: Create your child view
Create a view of the content of type 'product', and choose to create a block 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 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 example, we'll use a view name of 'products_by_category'. Once the view is
@ -36,16 +34,15 @@ 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). the filter value is not available (e.g. display contents of no results found).
Set your desired sort and save your view. Set your desired sort and save your view.
Step 2: Create your parent view ### Step 2: Create your parent view
Create a view of taxonomy terms of type 'product categories', and choose to 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 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. created, you should see the preview showing all the product categories. Choose
Choose to add a field of type "Term ID", and choose "Exclude from display"; this to add a field of type "Term ID", and choose "Exclude from display"; this is
is necessary to make the term id available to the next field which uses necessary to make the term id available to the next field which uses Twig Tweak.
Twig Tweak. Now, choose to add a field of type "Custom text" from the "Global" Now, choose to add a field of type "Custom text" from the "Global" category.
category. Inside that field, enter the Twig Tweak call to display the child view Inside that field, enter the Twig Tweak call to display the child view we
we created above, passing the tid as a contextual filter, as such: created above, passing the tid as a contextual filter, as such:
```twig ```twig
{{ drupal_view('products_by_category', 'block_1', tid) }} {{ drupal_view('products_by_category', 'block_1', tid) }}
``` ```
@ -57,7 +54,7 @@ products within each.
This example can be applied to any nested view scenario, including This example can be applied to any nested view scenario, including
multiple-levels of nesting. multiple-levels of nesting.
Check if the view has results ## Check if the view has results
```twig ```twig
{% set view = drupal_view_result('related', 'block_1')|length %} {% set view = drupal_view_result('related', 'block_1')|length %}
{% if view > 0 %} {% if view > 0 %}

Loading…
Cancel
Save