Custom order for a loop in Grav CMS (uses Twig) - twig

I'm listing all the pages (from different folders) belonging to category "featured", and I'd like to order them by a custom value. At the moment they're being ordered by date:
{% for p in taxonomy.findTaxonomy({'category':'featured'}).order('date','desc') %}
{{ p.title }}
{% endfor %}
However I want to add an "order" field in the page header
---
// for the first page to show
title: Just a page
order: 1
---
// for the second page to show
title: Not just a page
order: 2
---
What I want is something like .order('p.header.order','asc')
Is there any way to do this?

I got help from #rhukster at Grav forums but posting the solution here as well. It's simple.
.order('header.order','asc')

grav has the option to order folders by adding a number at the beginning of the folder name.

Related

Shopware 6: logo automatically in documents

Is there a way to automatically output the logo of a sales channel in the documents (invoice, delivery bill, etc.) without having to create a separate document for each sales channel?
Thanks for your help :-)
Unfortunately, I have not found an approach so far.
I think it currently isn't possible to differentiate between sales channels with the document settings in the administration.
You could create a media custom field for the sales channel, upload the logo there and then use the custom field in the document template.
Go to Settings > System > Custom Fields
Add a new set and assign it to Sales Channels
Within the new set create a new custom field
As type choose Media and think of a unique technical name
In the sidebar to the left go to the sales channel you want to upload a logo for
Scroll down to the custom fields of the sales channel and upload the logo with the new media custom field
Save the sales channel
Then you'll need a plugin to extend the document template. Within your plugin create the template extension e.g. at {pluginRoot}/src/Resources/views/documents/base.html.twig
with the content:
{% sw_extends '#Framework/documents/base.html.twig' %}
{% block document_header %}
{% if context.salesChannel.customFields.custom_test_media is defined %}
{% set media = searchMedia([context.salesChannel.customFields.custom_test_media], context.context) %}
{# #var item \Shopware\Core\Content\Media\MediaEntity #}
{% for item in media %}
<img src="{{ item.url }}" class="logo"/>
{% endfor %}
{% endif %}
{% endblock %}
with custom_test_media being the technical name of the media custom field you created earlier.
Here is a video how to change Shopwares Document Logos the easy way using the WYSIWYG Document Editor:
https://youtu.be/fGBMDmVMPvA?t=162
The easiest way to change your documents is by customizing them with the WYSIWYG Document Editor. The live preview will save you a lot of time and money in comparison to going back and forth 1000 times between making adjustments in your Twig Templates and generating new PDFs for testing.
Check it out:
https://store.shopware.com/appli81810362453/wysiwyg-dokumenten-editor-pdf-rechnungen-lieferscheine-gutschriften-stornos-designen.html
I am the developer, which created the WYSIWYG Document Editor Shopware 6 App. Feel free to ask me any questions about the App. I am happy to help you.

Shopware 6: How to display product name in breadcrumbs

We want to add the product name to the breadcrumb list when current page matches a product detail page.
Unfortunately, it doesn't seem like the product is loaded yet when the breadcrumb is rendered. We have tried using dump() to see what variables are available and nothing related to the product.
Where should I look in order to include the product name in our breadcrumbs?
Have a look at storefront/base.html.twig. There you will see, that currently the breadcrumb-template gets passed the context and the category. If you want to also use some product-information, you have to overwrite this block like this:
{% block base_breadcrumb %}
{% sw_include '#Storefront/storefront/layout/breadcrumb.html.twig' with {
context: context,
category: page.product.seoCategory,
product: page.product
} only %}
{% endblock %}
Then you can use product in the breadcrumb-template.

Render a link field inside a paragraph template drupal 8

I have a paragraph called link. In this paragraph there is a single link field that allows multiple values. In the paragraph-link.html.twig file I want to render all of the links added to the paragraph. Instead I get the same link duplicated as many times as the number of link values. So if I add two links, it renders the first link twice. I also need it to work with external and internal links. Currently it only renders the external links properly (but only ever renders the first one).
{% for item in paragraph.field_link %}
{{ paragraph.field_link.title }}
{% endfor %}
Thanks for the comments, the following is working for external links but not internal ones. Internal link URI renders as "internal:/"
{% for item in paragraph.field_link %}
{{ item.title }}
{% endfor %}
With the help of the comments I was able to get this working as I wanted within the paragraph template. Below is the working code for a Link field that allows multiple values within a paragraph field.
{% for item in paragraph.field_link %}
{{ item.title }}
{% endfor %}

Pagination works for archive of CPT but throws 404 error for single-cpt.php

I can use Timber pagination in archive-law.php for the archive page (/law/page/2) of my custom post type, "law", but with the same codes in single-law.php, the second page (/law/cpt-post-slug/page/2) shows the 404 error. How can I fix that?
That’s how it’s supposed to work. Pagination only works for archive templates like archive.php or home.php, where you have a query with multiple results that can be paginated. It will not work for singular templates like single.php.
However, you could use next_post_link() and previous_post_link(), which display a link to the next or previous post which exists in chronological order from the current post.
In Timber, these functions are available as post.next and post.prev. Here’s an example for how you could use it in in a singular template in Twig:
{% if post.next %}
<h3>Next Article</h3>
{{ post.next.title }}
{% endif %}

Grav CMS: how to show/hide parts of the page depending on conditions?

The Grav's documentation clearly describes how a whole page or a folder could be hidden from unregistered users. It also describes how a whole page could be seen only by particular user groups.
But what about pieces of a page, let's say, some links or a private info I want to show on some conditions?
Ok, for registered users I found a snippet at Login plugin docs:
{% if grav.user.authenticated %}
content for registered users goes here
{% endif %}
But going wider - how can I show/hide pieces of a particular page depending on some custom logic in PHP code, i.e. not necessarily user related?
I'm thinking about a twig/shortcode plugin, something like:
{% if some.custom.condition.or.PHP.function %}
hidden content goes here
{% endif %}
or
[hidden_if_something] hidden content goes here [/hidden_if_something]
But not sure how exactly this should be implemented. So working examples would be appreciated. Thanks.
There is a recipe in the Grav documentation here. This provides an example of how to render the output of a PHP code result in a twig template.
In the example they create a plugin, and implement a twig extension providing access to a php function. They can then simply call that php function like in a twig template.
{{ example() }}
Following that example, you can implement whatever logic you would like in php, and call the function in a twig if statement.
{% if example() == true %}
your conditional output
{% endif %

Resources