How to access view fields inside page--%.html.twig - drupal

I have a page view that displays title and body fields and the twig template for the view is named as "page--my--view.html.twig". The view content is as follows:
{% if page.content %}
{{ page.content }}
{% endif %}
How can I add individual fields and classes in this template? For example:
<h3 class="title">{{ fields.title.content }} </h3>
I want to add a 'for' loop and print all the titles through page template like above. How to do this?

Related

Rendering Product in Shopify Blog Post Article using card-product.liquid

I'm trying to show products card inside blog posts / articles of shopify.
I've edited main-article.liquid so that when the content of an article is rendered, it checks the content for a particular string and then splits the code.
if a split has occurred than it isolates the handle of the product.
then it will need to render the product based on the product handle as a card-product.
card-product is the element in which a product is show in a collection ( as in a catalog list )
though I cannot render the product but just a dummy element as in this
I've followed 2 tutorials to get this far:
happypoint
thefunction
the first one is using a snippet that is not present in my theme ( dawn ) but also adding it just to se some results brings out same missing content problem but in a different design.
{%- when 'content'-%}
<div class="article-template__content page-width page-width--narrow rte" itemprop="articleBody" {{ block.shopify_attributes }}>
{% assign divider = '[PROD]' %}
{% assign dividerClose = '[/PROD]' %}
{% assign text = article.content | split: divider %}
{{ text[0] }}
{% for divider in text offset:1 %}
{% assign x = forloop.index %}
{% assign newtext = text[x] | split: dividerClose %}
{% assign productHandle = newtext[0] %}
{% assign product = all_products[productHandle] %}
<div class="product_item">
{% render 'card-product',
media_aspect_ratio: section.settings.image_ratio,
show_secondary_image: section.settings.show_secondary_image,
show_vendor: section.settings.show_vendor,
show_rating: section.settings.show_rating,
lazy_load: lazy_load,
show_quick_add: section.settings.enable_quick_add,
section_id: section.id
%}
</div>
{{ newtext[1] }}
{% endfor %}
</div>
How can I render the product as a card-product.liquid?

Twig/Timber/ACF how to output only categories assigned to item

I have a page built in Timber/Twig (WordPress) using ACF.
It outputs a list of people using an ACF repeater field, and I need to filter these using categories, which are meant to be set in one of the fields (using taxonomy as its type) inside the repeater.
The problem I am having is that all of the categories are being output, rather than the ones which are selected via an ACF checkbox for each person.
Here is my code:
{% for item in post.meta('projectname_network') %}
{% set counter = ( counter | default(0) ) + 1 %}
<div class="{% for category in post.terms('category') %}{{ category.slug }} {% endfor %} portfolio-item network-headshot col-lg-2 col-md-3 col-sm-4 col-6">
<div class="network-header" id="network{{ counter ~ item.title }}">
{% for category in post.terms('category') %}
{{ category.slug }}
{% endfor %}
<div data-toggle="modal" data-target="#modal{{ counter ~ item.title }}" aria-controls="modal{{ counter ~ item.title }}">
other content goes here
</div>
</div>
</div>
{% endfor %}
I am outputting the categories as class names for the bootstrap filter, but have also included them as text, so I can more easily see what's being output.
I know that right now I am basically saying include all categories from the post, or all categories that exist, but that's the nearest I've got to displaying what I need.
If I put:
{% for category in projectname_category %}
{{ category.slug }}
{% endfor %}
then nothing is output (projectname_category is the name of the ACF category field).
I'm just not sure of the correct syntax to use here, as ACF and Timber docs don't seem to cover this use case.

Display View in twig

I created (in Drupal 8) a view of a dataset (newsteaser_mit_bild) with some News in there.
With this view i created a block. The name is automatically generated (views_block__newsteaser_mit_bild_block_1).
The normal content is displayed with
{{ page.content }}
How can i display this View in my Twig File ?
{{ page.newsteaser_mit_bild }}
seems not to be right.
How can i use the view/block in my twig an how can i template them ?
In the main twig file you can use name block like this:
{% block my_custom_block }%
{% endblock my_custom_block %}
In the another twig file you can call the block like this:
{% extends 'link_for_file.twig' %}
{% block my_custom_block }%
{{ parent() }}
{% endblock my_custom_block }%
You can preprocess a new variable and use views_embed_view like:
function THEME-NAME_preprocess(&$variables, $hook) {
$variables['MY-VIEW-NAME'] = views_embed_view('VIEW-ID');
}
And then in the twig file:
{{ MY-VIEW-NAME }}

Iterate lists/content in block template twig Drupal 8

How would I be able to supersede the hierarchical dependencies in Drupal 8's twig engine to be able to loop within the i.e Lists/Views which is assigned to a block. So we would have a template: block--views-block--[machine-name]-1.html.twig You will be required to have the variable {{ content }}
Which then recursively buries itself down to field templates. Its completely killing me that one would need so many levels to produce on block of content.
I would like to iterate within the top custom block template the list.
Attempted
{% for key, value in _context %}
<li>{{ key }}</li>
{% endfor %}
To evaluate what is available to iterate down into the object but with no luck. I did though find a nice overriding object structure to reach the field attributes but that was within the field level
item.content['#item'].entity.uri.value
Thanks
i use this to "generate" a picture from my
node--news--full.html.twig
<div class="col-md-3">
{{ content.field_newsbild }}
</div>
the twig debug suggests some filenames. i took this:
field--node--field-newsbild--news.html.twig
and in there i wrote:
{% for item in items %}
<img alt="" src="{{ file_url(item.content['#item'].entity.uri.value) }}" class="img-responsive" {{ attributes }} >
{% endfor %}
hope i'll help a bit.

Symfony2 - access form from a widget block template

Following is how the form is rendered.
<fieldset class="properties">
{% block form_content %}
{{ form_widget(form) }}
{% endblock %}
</fieldset>
Now I can access any form field in this template, like {{ form.description }}, it's all good . But here I have a collection field in this form, let's call it collection, I have built a custom field type for this, the block template for this custom type is customCollect_widget, everything until this point is fine, but if I want to access the collection object in this widget template, I got an error saying the field name does not exist in the form object.
Here's my widget template:
{% block customCollect_widget %}
{% spaceless %}
{% for aa in form.collections %}
<div>something</div>
{% endfor %}
....
<% endblock %}
The problem, as I figured, is that the form isn't the same object that's passed to the code above. Is there any workaround to it?
Ha, I solved it. In the collection type widget template, the form variable is referencing the form field type itself, in this case, the collection type. So to loop through the collection in the widget block, you just do {% for child in form %}.

Resources