How to get {{ pages }} object in a modular page - grav

Any thoughts on how to get the pages object in a modular page?
{{ dump(pages) }} returns null, but if I put the same statement in a regular page it works fine.
I have a page field that only gives me the URL of the selected pages and I need to get their title, ideally by just doing pages.find(link).title but "pages" is null for some reason.

For using page find, you have to use the singular page, not pages.
example:
{{ page.find('/contact').title }}
That said, you being not able to access the pages object from a modular page is probably a bug that should be reported as I see no reason why this would not work.

Related

Drupal 8 Twig Page Fields

So I'm just getting started with Drupal and Drupal 8 and have a hard time understanding accessing twig content. Ideally what I would like to do is require some fields using the structured content.
When a type of structured content is used I would like to load a specific twig template and access the fields by machine readable name. This will allow me to setup template types with specific requirements for content.
I'm struggling with 2 parts and maybe what I want to do isn't possible in drupal. The first part how do I assign a page template based on structured content type?
The second issue is how do print out specific fields. I'm able to print all fields using {{page.content}} but {{page.content.field_name}} prints nothing. I'm very confused how to proceed forward. I know I can use modules and assign them to sections but for structured data types this won't allow for rigid enforcement of data collection like structured content. Some of this content flows outside a single content area so I'd really like to do it in a main layout file.
Thoughts?
For defining page template based on content type:
You can have:
page--front.html.twig - For front page
page--user.html.twig - For user page
page--youtube_videos.html.twig - For content type (here it is: youtube_videos)
For print content fields, you need to goto node.html.twig
For image field: {{ content.field_image }}
For title: {{ label }}
Tag field: {{ content.field_tags }}

How to display errors in overriden FOSUserBundle Registration form?

I have overridden the registration .twig files and it works except when the user doesn't enter matching passwords. How do I display any errors that are thrown by the registration process?
You can render your form errors by using form_errors function, like this:
{{ form_errors(form) }}
{{ form_errors(form.someField) }}
You should read this official documentation How to Customize Form Rendering to understand in deep how Symfony rendering your form view and how to custom the template as you want.

Output image field in Twig and D8

I created a basic page with a NID of 176. My basic page content type contains a field called field_banner_image.
In my templates directory I created a file called page--node--176.html.twig and the template works.
I saw a few threads where you can access the node content via {{ content.field_name }}, however, my content appears to NULL every time.
I managed to output the URL of my image field via:
{{ file_url(node.field_banner_image.entity.uri.value) }}
I feel like there has to be an easier/better way of doing this. Any suggestions? Why is content null in my twig templates?
If your template is named page--node--176.html.twig, it is actually a page template overriding the base page template page.html.twig, so you will have access to all page variables but not those in your node.
Although your node type is called "page", it is still a node, and the template for it would be node--176.html.twig which overrides node.html.twig.
In your node--***.html.twig you will have access to the content variable. Let me know if you need help.

Twig - Get URL for canonical tag

I'm looking to create a dynamic rel="canonical" tag in my application which pulls in the current URL but want to ensure any query parameters are removed. E.g http://www.example.com/test/?page=2 should have the canonical as http://www.example.com/test/, therefore {{ app.request.uri }} doesn't work as this pulls in ?page=2 as well.
Does anyone know how to pull in the absolute path of a page without the query parameters?
This will work,
{{ url(app.request.attributes.get('_route'), app.request.attributes.get('_route_params')) }}
I just tried to dump baseUrl and can confirm that it does not work.
However, this works:
{{ app.request.getSchemeAndHttpHost ~ app.request.baseUrl ~ app.request.pathInfo }}
I know, it's not pretty but it does the job :)

How to show Next & Previous page links in Django-CMS

We've a site built in Django-CMS and have developed a mobile version with alternative CSS to suit the smaller viewing area. As well as the usual navigation bar we want to include Next and Previous page links at the bottom of each page.
I know how to output the current page's siblings using this code:
{% show_menu current_page.level %}
What is the easiest way to output links to the next and previous page?
You can use {{ request.current_page.get_next_sibling }} and {{ request.current_page.get_previous_sibling }} in your templates to show the 'neighbor' pages (not that either or b
You can use the methods get_next_filtered_sibling and get_previous_filtered_sibling - but probably only for newer versions of the django cms.
Here are two template tags that return page objects which you might want to feed into the {% page_url ... %} template tag.
#register.assignment_tag(takes_context=True)
def get_next_page(context):
current_page = context['request'].current_page
return current_page.get_next_filtered_sibling(
publisher_is_draft=False
)
#register.assignment_tag(takes_context=True)
def get_prev_page(context):
current_page = context['request'].current_page
return current_page.get_previous_filtered_sibling(
publisher_is_draft=False
)

Resources