Iterating over ArrayEntity - twig

We were have this backend code:
$event->getPage()->addExtension('downloads', new ArrayEntity($downloads));
In the twig
{{ dump(page.extensions.downloads) }}
shows the Array entity.
Before the Shopware updates
This loop worked:
{% for key, files in page.extensions.downloads.data %}
But now it doesn't (data is null)
Also
{% for key, files in page.extensions.downloads %}
does not work.
How to iterate over the array entity in twig?

{% for key, files in page.extensions.downloads.all %}
works

Related

How to add entity extension in Shopware 6 App?

I want to query the categories of the product in the product listing of a Shopware app, so that I can query the customFields of all categories. Is this even possible with an app?
I have already tried via a navigation-page-loaded. And when I override the box-standard.html.twig and access the product, I can't access the categories there.
For each product i want the categories extensions
As the categories association is not loaded for products in the listing you have to fetch the categories using an app script.
As already noted, add the script to the navigation-page-loaded hook, i.e. in Resources/scripts/navigation-page-loaded/category-loader.twig:
{% set products = [] %}
{% if hook.page.cmsPage.type === 'product_list' %}
{% foreach hook.page.cmsPage.sections as section %}
{% foreach section.blocks as sectionBlock %}
{% if sectionBlock.type !== 'product-listing' %}
{% continue %}
{% endif %}
{% foreach sectionBlock.slots as slot %}
{% if slot.type !== 'product-listing' %}
{% continue %}
{% endif %}
{% foreach slot.data.listing.entities as product %}
{% set products = products|merge([product]) %}
{% endforeach %}
{% endforeach %}
{% endforeach %}
{% endforeach %}
{% endif %}
{% set categoryIds = products|reduce((carry, v) => carry|merge(v.categoryIds), []) %}
{% if categoryIds %}
{% set categories = services.repository.search('category', {'ids': categoryIds}) %}
{% foreach products as product %}
{% do product.addArrayExtension('myCategories', {
'categories': categories.entities.getList(product.categoryIds),
}) %}
{% endforeach %}
{% endif %}
Where we first extract all the products, than load the categories of all products at once, and than assign the categories back to the products.
Note that for reading the category entity you need the correct permission, i.e. in the manifest.xml add:
<permissions>
<read>category</read>
<read>category_translation</read>
</permissions>
Now you should be able to access in the box-standard.html.twig template the categories using product.extensions.myCategories.categories.
If I understand your issue correctly, you are trying to access a product's category's custom fields in some piece of storefront logic (like a Twig template).
The way I see it, you would have to add a script that would enable you to query the repository for the categories and their custom fields. The issue is, a SalesChannelProductEntity will not contain the information about all its categories (only the SeoCategory), so you might need to first query the product_category repository.
Generally, it is going to be complicated but it should be doable.

html_entity_decode for twig (opencart)

im trying to output an attribute of a product on my product page (opencart v3).
The attribute is called 'technicaldetails' and it works just fine using this code:
{% if attribute_groups %}
{% for attribute_group in attribute_groups %}
{% if attribute_group.name == 'technicaldetails' %}
{% for attribute in attribute_group.attribute %}
{{ attribute.text }}
{% endfor %}
{% endif %}
{% endfor %}
{% endif %}
but the technical details field have unstyled list stored in it.. and this outputs the complete html instead of rendering the list.
ive tried using {{ attribute.text|e }} and {{ attribute.text|raw }} and many other alternatives i could find.. but each time is just throws out the html and not render it..
in php this used to work.
<?php echo html_entity_decode($attribute['text']); ?>
so how can i decode the html now as i cant use php in twig and there is no html_entity_decode in twig either :(
looking forward for somehelp :)
much appreciated
thanks.
Just register the html_entity_decode function in twig.
The most simple way is to look where twig is loaded and add the following code,
$twig->addFilter(new \Twig_Simple_Filter, 'html_entity_decode', 'html_entity_decode');
After that you can just do the following in your twig templates
{{ attribute.text|html_entity_decode }}
UPDATE: For Opencart 3.0.3.7 version filter should be like this:
$twig->addFilter(new \Twig\TwigFilter('html_entity_decode','html_entity_decode'));
Find file
document_root/system/library/template/twig.php
Just after
$this->twig = new \Twig_Environment($loader, $config);
add following code
$twig->addFilter(new \Twig_SimpleFilter('html_entity_decode', 'html_entity_decode'));
After doing this, you must went to admin to reload all modifications in menu Extensions -> modifications.
After that you can do the following in all twig files *.twig
{{ attribute.text|html_entity_decode }}

loop through key values of an array in twig

I have not been able to find my answer elsewhere (maybe because I didn't know how to ask google as I'm pretty new to this ;))
I'm working with symfony and twig.
I pass an array in my view with only one entry related to the id. It looks like this in my view
array:2 [▼
"sponsor" => Sponsor {#473 ▼
-id: 5
-sponsorCode: "FUT"
-name: "MANULO"
-city: "OLERDOLA"
-zipCode: 0
-address: ""
-country: "ESPANA"
-phoneNumber: 32767
-email: ""
-creationDate: DateTime {#470 ▶}
}
"app" => AppVariable {#476 ▶}
]
I know I can access each property by doing
{{sponsor.name}}
But I'm trying to do it through a loop for each field of this array
something like
{% for key, value in sponsor %}
<div class="field-group">
<div class="field">{{ key }}:</div>
<div class="value">{{ value }}</div>
</div>
{% endfor %}
Am I missing something?
Thank you very much
From the TWIG documentation:
Keys Only
By default, a loop iterates over the values of the sequence. You can iterate on keys by using the keys filter:
<h1>Members</h1>
<ul>
{% for key in users|keys %}
<li>{{ key }}</li>
{% endfor %}
</ul>
Keys and Values
You can also access both keys and values:
<h1>Members</h1> <ul>
{% for key, user in users %}
<li>{{ key }}: {{ user.username|e }}</li>
{% endfor %} </ul>
https://twig.sensiolabs.org/doc/2.x/tags/for.html
Keep your eye on the TWIG documentation, its rather comprehensive.
Looking at your code, it looks ok. However, the issue could be that the {{value}} may need further identification, such as {{ value.id }}

Symfony twig use keys in for loops

Does somebody know an other way to do that with twig, because it returns me an error :/
{% for key, conversation in conversations %}
{% set lastMessage = sortedConversations.key %}
<p>{{ lastMessage }}</p>
{% endfor %}
Here is the error returned :
Key "key" for array with keys "0" does not exist
Thanks !
I'm not sure to understand, but may be you can try this:
sortedConversations[key]
instead of
sortedConversations.key
Try this:
{% for key, conversation in conversations %}
{% set lastMessage = sortedConversations[key] %}
<p>{{ lastMessage }}</p>
{% endfor %}
Note the brackets around key.
This way, twig should notice key as a variable and not as a simple string.

Twig get object names ending with numbers dynamically

I have some objects like address1, address2, address3 ... address10. All these objects have lat and long values.
I know we can get it from the attribute() function of Twig, but what I want in my twig template is to get the main objects
{% for i in 1..10 %}
{% set address = address~i %}
{{ address.lat }}
// or like
{{ attribute(address, 'lat') }}
{% endfor %}
Just simply use :
{% for i in 1..10 %}
{{ attribute(attribute(_context, 'address'~i), 'lat') }}
{% endfor %}

Resources