Controllers¶
Custom rendering logic¶
In some cases, displaying a Content item/Location via the built-in ViewController
is not sufficient to show everything you want. In such cases you may want to use your own custom logic to display the current Content item/Location instead.
Typical use cases include access to:
- Settings (coming from
ConfigResolver
orServiceContainer
) - Current Content item's
ContentType
object - Current Location's parent
- Current Location's children count
- Main Location and alternative Locations for the current Content item
- etc.
There are three ways in which you can apply a custom logic:
- Configure a custom view controller alongside regular matcher rules (recommended).
- Add a Symfony Response listener to add custom logic to all responses.
- Override the built-in
ViewController
with the custom controller in a specific situation.
Permissions for custom controllers
See permission documentation for information about access control for custom controllers.
Enriching ViewController with a custom controller¶
This is the recommended way of using a custom controller
To use your custom controller on top of the built-in ViewController
you need to point to both the controller and the template in the configuration, for example:
1 2 3 4 5 6 7 8 9 10 11 |
|
With this configuration, the following controller will forward the request to the built-in ViewController
with some additional parameters:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
|
These parameters can then be used in templates, for example:
1 2 3 4 5 6 7 8 |
|
Adding a listener¶
One way to add custom logic to all responses is to use your own listener. Please refer to the Symfony documentation for the details on how to achieve this.
Using only your custom controller¶
If you want to apply only your custom controller in a given match situation and not use the ViewController
at all, in the configuration you need to indicate the controller, but no template, for example:
1 2 3 4 5 6 7 8 9 10 |
|
In this example, as the ViewController
is not applied, the custom controller takes care of the whole process of displaying content, including pointing to the template to be used (in this case, full/custom_controller_folder.html.twig
):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
|
Here again custom parameters can be used in the template, e.g.:
1 2 3 4 5 6 7 8 9 10 11 |
|
Query controller¶
The Query controller is a predefined custom content view controller that runs a repository Query.
You can use it as a custom controller in a view configuration, alongside match rules. It can use properties of the viewed Content item or Location as parameters to the Query.
The Query controller makes it easy to retrieve content without writing custom PHP code and to display the results in a template. Example use cases include:
- List of Blog posts in a Blog
- List of Images in a Gallery
Built-in Query Types¶
You can make use of the following built-in Query Types for quick rendering:
Children¶
The Children
Query Type retrieves children of the given Location.
It takes location
or content
as parameters.
1 2 3 4 5 6 |
|
Siblings¶
The Siblings
Query Type retrieves Locations that have the same parent as the provided Content item or Location.
It takes location
or content
as parameters.
1 2 3 4 5 6 |
|
Ancestors¶
The Ancestors
Query Type retrieves all ancestors (direct parents and their parents) of the provided Location.
It takes location
or content
as parameters.
1 2 3 4 5 6 |
|
RelatedToContent¶
The RelatedToContent
Query Type retrieves content that is a reverse relation to the provided Content item.
It takes content
or field
as obligatory parameters.
field
indicates the Relation or RelationList Field that contains the relations.
1 2 3 4 5 6 7 |
|
GeoLocation¶
The GeoLocation
Query Type retrieves content by distance of the location provided in a MapLocation Field.
It takes the following obligatory parameters:
field
- MapLocation Field identifierdistance
- distance to check forlatitude
andlongitude
- coordinates of the location to check distance to- (optional)
operator
- operator to check value against, by default<=
1 2 3 4 5 6 7 8 9 10 |
|
General Query Type parameters¶
All built-in Query Types take the following optional parameters:
limit
- limit for number of search hits to returnoffset
- offset for search hits, used for paging the resultssort
- sorting optionsfilter
- additional query filters:content_type
- return only results of given Content Typesvisible_only
- return only visible results (defaulttrue
)siteaccess_aware
- return only results limited to current SiteAccess root (defaulttrue
)
For example:
1 2 3 4 5 6 7 8 9 10 11 12 |
|
Sorting order
To provide sorting order to the sort
parameter, use names of the Sort Clauses in
eZ\Publish\API\Repository\Values\Content\Query\SortClause
.
Using built-in Query Types¶
This example assumes a "Blog" container that contains a set of "Blog post" items. The goal is, when viewing a Blog, to list the Blog posts it contains.
Two items are required:
- a View template - It will render the Blog, and list the Blog posts it contains
- a
content_view
configuration - It will instruct Platform, when viewing a Content item of type Blog, to use the Query Controller, the view template, and theChildren
QueryType. It will also map the ID of the viewed Blog to the QueryType parameters, and set which Twig variable the results will be assigned to.
The content_view
configuration¶
We now need a view configuration that matches Content items of type "Blog", and uses the QueryController to fetch the blog posts:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
The view's controller action is set to the QueryController's contentQuery
action (ez_query::contentQueryAction
). Other actions are available that run a different type of search (contentInfo or location).
The QueryController is configured in the query
array, inside the params
of the content_view
block:
query_type
specifies the QueryType to use, based on its name.parameters
is a hash where parameters from the QueryType are set. Arbitrary values can be used, as well as properties from the currently viewed Location and ContentInfo. In that case, the ID of the currently viewed Location is mapped to the QueryType'slocation
parameter:location: '@=location'
assign_results_to
sets which Twig variable the search results will be assigned to.
The view template¶
Results from the search are assigned to the blog_posts
variable as a SearchResult
object. In addition, since the standard ViewController is used, the currently viewed location
and content
are also available.
1 2 3 4 5 |
|
Creating custom Query Types¶
Beyond the built-in Query Types, you can create your own.
For example, this Query Type returns a Query that searches for the last ten published Content items, ordered by reverse publishing date.
It accepts an optional contentType
parameter [lines 13-15] that can be set to a Content Type identifier (e.g. article
):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
|
In content view configuration, use the name of the Query Types as defined in getName
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
Your Query Type must be registered as a service.
Configuration details¶
controller
¶
Following Controller actions are available:
locationQueryAction
runs a Location SearchcontentQueryAction
runs a content SearchcontentInfoQueryAction
runs a ContentInfo searchpagingQueryAction
returns aPagerFanta
object and can be used to quickly paginate query results
See the Search documentation page for more details about different types of search.
params
¶
The Query is configured in a query
hash in params
, you could specify the QueryType name, additional parameters and the Twig variable that you will assign the results to for use in the template.
query_type
- Name of the Query Type that will be used to run the query, defined by the class name.parameters
- Query Type parameters that can be provided in two ways: 1. As scalar values, for example an identifier, an ID, etc. 1. Using the Expression language. This simple script language, similar to Twig syntax, lets you write expressions that get value from the current Content item and/or Location: - For example,@=location.id
will be evaluated to the currently viewed Location's ID.content
,location
andview
are available as variables in expressions.assign_results_to
- This is the name of the Twig variable that will be assigned the results.
- Note that the results are the SearchResult object returned by the SearchService.
Query Type objects¶
Query Type is an object that builds a Query. It is different from Public API queries.
To make a new Query Type available to the Query Controller, you need to create a PHP class that implements the QueryType interface, then register it as such in the Service Container.
For more information about the Service Container on its documentation page.
The QueryType interface¶
The PHP QueryType interface describes three methods:
getQuery()
getSupportedParameters()
getName()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
Parameters¶
A QueryType may accept parameters, including string, array and other types, depending on the implementation. They can be used in any way, such as:
- customizing an element's value (limit, Content Type identifier, etc.)
- conditionally adding/removing criteria from the query
- setting the limit/offset
The implementations should use Symfony's OptionsResolver
for parameter handling and resolution.
Naming of QueryTypes¶
Each QueryType is named after what is returned by getName()
. Names must be unique. A warning will be thrown during compilation if there is a conflict, and the resulting behavior will be unpredictable.
QueryType names should use a unique namespace, in order to avoid conflicts with other bundles. We recommend that the name is prefixed with the bundle's name, e.g.: AcmeBundle:LatestContent
. A vendor/company's name could also work for QueryTypes that are reusable throughout projects, e.g.: Acme:LatestContent
.
Registering the QueryType into the service container¶
QueryTypes must be registered as Symfony Services and implement the eZ\Publish\Core\QueryType\QueryType
interface.
A QueryType
service is registered automatically when autoconfigure: true
is set either for that service or in _defaults
.
Tip
In the default eZ Platform installation services are autoconfigured by default for the App
namespace,
so no additional registration is required.
Otherwise, you can register your QueryType with a service tag:
1 2 3 |
|
Paginating with QueryTypes¶
Using the ez_query::pagingQueryAction
you can quickly get paginated results of a query:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
limit
defines how many query results are listed on a page.
Tip
See Content view configuration for details on this configuration.
Pagination controls are provided in the pagerfanta
Twig function:
1 2 3 4 |
|
The OptionsResolverBasedQueryType abstract class¶
An abstract class based on Symfony's OptionsResolver
makes the implementation of QueryTypes with parameters easier.
It provides final implementations of getQuery()
and getDefinedParameters()
.
A doGetQuery()
method must be implemented instead of getQuery()
. It is called with the parameters processed by the OptionsResolver, meaning that the values have been validated, and default values have been set.
In addition, the configureOptions(OptionsResolver $resolver)
method must configure the OptionsResolver.
The LatestContentQueryType from the example above can benefit from the abstract implementation:
- validate that
type
is a string, but make it optional - validate that
limit
is an int, with a default value of 10
Note
For further information see the Symfony's Options Resolver documentation page
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
|
Using QueryTypes from PHP code¶
All QueryTypes are registered in the QueryType registry, which is a Symfony Service injectable as ezpublish.query_type.registry
:
1 2 3 4 5 6 |
|
Example of PHP code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
|
Content query Field Type¶
The Content query Field Type enables you to configure a content query that will use parameters from a Field definition. The results will be available in a Content item's Field.
Use it by adding a Content query Field Type to your Content Type.
Select a predefined Query Type from a list and provide the parameters that are required by the Query Type, e.g.:
1 |
|
Select the Content Type of items you want to return in the Returned type dropdown.
To take it into account, your Query Type must filter on the Content Type.
Provide the selected Content Type through the returnedType
variable:
1 |
|
The following variables are available in parameter expressions:
returnedType
- the identifier of the Content Type selected in the Returned type dropdowncontent
- the current Content itemcontentInfo
- the current Content item's ContentInfomainLocation
- the current Content item's main Location
Pagination¶
You can paginate the query results by checking the Enable pagination box and selecting a limit of results per page.
The following optional parameters are available:
enablePagination
- when true, pagination will be enabled even if it is disabled in the Field definitiondisablePagination
- when true, pagination will be disabled even if it is enabled in the Field definitionitemsPerPage
- limit of results per page, overriding the limit set in Field definition. It is required ifenablePagination
is set to true and pagination is disabled in the Field definition
For example:
1 |
|
You can also define an offset for the results. Provide the offset in the Query Type, or in parameters:
1 |
|
If pagination is disabled and an offset value is defined, the query's offset is added to the offset calculated for a page
(for example, with offset = 5
and itemsPerPage = 10
, the first page starts with 5, the second page starts with 15, etc.).
Without offset defined, pagination defines the starting number for each page
(for example, with itemsPerPage = 10
, first page starts with 0, second page starts with 10, etc.).
Content query Field Type view¶
Configure the Content query Field Type's view using the content_query_field
view type:
1 2 3 4 5 6 7 |
|
Query results are provided to the template in the items
variable:
1 2 3 4 5 6 7 |
|
The default view type is line
, defined under itemViewType
.
You can change it by passing a different view to viewType
in the template, e.g.:
"viewType": "list"
.
The isPaginationEnabled
parameter indicates if pagination is enabled.
When pagination is enabled, items
is an instance of PagerFanta:
1 2 3 4 5 6 7 |
|
pageParameter
contains the page parameter to use as the PagerFanta pageParameter
argument.