eZ Platform Discussions

Query available to pagelayout in views.yml?


In views.yml, is it possible to assign params and a querytype to the main pagelayout? Like so?

    pagelayout: "pagelayout.html.twig"
                query_type: "SitewideAlert"
                assign_results_to: "sitewide_alert"

I’m looking to implement a site-wide alert message, that would be available on all pages of the site.

When those params are in place in views.yml, I get “The file does not contain valid YAML”. So either there’s something wrong with my structure, or this is not an accepted setting for the pagelayout.


Update: I assumed this was not possible and looked for an alternative. Just creating a controller to do this instead.

Might be helpful for this to be clear in the documentation.


Thanks for the suggestion on documentation, I’ll have someone check this.

Did your solution work, creating a controller? If so, you could mark your topic as solved (this helps other members).


What aspect do you think is worth documenting ? That query results can’t be assigned by a view to the layout ? Actually, to some extent they do: view templates extend the pagelayout. You can customize blocks that exist in it, such as headers or a particular block.

Or would you document in the pagelayout section that adding “variables” to it should be done using a custom controller, with a link to an example ?


I’d probably document that this ability to add queries is limited to content_views only, and can’t be directly applied to pagelayout via the views.yml file.

The solution I found was to use a controller to access the query type

class SitewideAlertController extends ContainerAwareCommand
	public function getQuery(array $parameters = [])
		$queryType     = $this->getContainer()->get('ezpublish.query_type.registry')->getQueryType('AppBundle:SitewideAlert');
		$query         = $queryType->getQuery();

		$searchResults = $this->getContainer()->get('ezpublish.api.service.search')->findContent($query);

And then access it in pagelayout.html.twig like so:

{{ render_esi(controller(
    {  }
)) }}

Note that I can’t attest to the correctness of this solution. But it or an equivalent solution might be helpful to include in the docs.