the ez platform view layer – ez conference 2016
Post on 21-Jan-2017
90 Views
Preview:
TRANSCRIPT
1
The eZ PlatformView Layer
Bertrand Dunogier Platform Lead bd@ez.no @bdunogier
This presentation is online: bdunogier.github.io/talks/ezconf2016-ezplatform-views
2
About me
11th year at eZ, 13th year around itPlatform backend lead engineerFocused on REST & Symfony layers
3
Plan
I. The View LayerII. Changes in Platform 1.xIII. Query typesIV. The Query ControllerV. Using the View layer in your projects
4 . 1
The View Layer
A layer that wraps around Symfony's MVC. For a given route, it loads aValue and renders it. It is mainly used to render content using UrlAliasesand ez_content:viewAction.
The template and controller can be customized using view configuration:
ezpublish: system: site: content_view: full: article: template: 'content/view/full/article.html.twig' controller: 'app.article_controller:viewArticleAction' match: Identifier\ContentType: 'article'
4 . 2
How it worked in eZ Publish 5.x
Two types of views: location_view and content_view.public function showArticleAction( $locationId, $viewType, $layout = false, array $params = array{ return $this->get( 'ez_content' )->viewLocation( $locationId, $viewType, $layout, ['some_variable' => 'some_value'] + $params );}
4 . 3
Drawbacks
Requires complex callsError proneTight coupling with the extended view controller, complex BC
4 . 4
4 . 5
What has changed in Platform 1.x
location_view is deprecated, and only content_view should beused**.Custom controllers do not need to inherit from a built-in one anymore.
namespace AppBundle\Controller;
class ArticleController{ public function viewArticleAction(ContentView $view) { $view->addParameters(['some_value' => 'whatever']);
return $view; }}
Parameters will be made available in the view template.
5 . 1
Query types
Added in eZ Platform 1.x.
They are named Query factories that support parameters, and can beregistered either by naming convention, or using a service tag.
$query = $queryType->getQuery(['paramName' => $paramValue]);$searchService->findLocations($query);
5 . 2
Use-case: LocationChildren query type
A QueryType that builds a LocationQuery searching for children of a givenlocation id.
In addition to the parentLocationId parameter, it optionally acceptslimit and offset:
$query = $queryType->getQuery(['parentLocationId' => 2, 'limit' => 5]);$searchService->findLocations($query);
5 . 3
LocationChildren QueryType structure
All QueryTypes must implement the\eZ\Publish\Core\QueryType\QueryType interface, that has threemethods:
namespace AppBundle\QueryType;
class LocationChildrenQueryType implements QueryType{ public function getQuery(array $parameters = []): Query
public function getSupportedParameters(): array
public static function getName(): string}
5 . 4
The getSupportedParameters() method
Returns the supported parameters names.
public function getSupportedParameters(){ return ['parentLocationId', 'offset', 'limit'];}
5 . 5
The getName static method
Returns the name of the QueryType.public function getName(){ return 'app.location_children';}
(Thank you Mr. Obvious)
5 . 6
The getQuery() method
Called to build the QueryReceives the build parametersReturns the Query object
public function getQuery(array $parameters = []){ return new LocationQuery([ 'filter' => new Criterion\ParentLocationId($parameters['parentLocationId']), 'offset' => $parameters['offset'] ?: 0, 'limit' => $parameters['limit'] ?: 10, ]);}
5 . 7
Using QueryTypes from PHP
inject a QueryType using its serviceuse the ezpublish.query_type.registry service
6 . 1
The Query controller
A built-in view controller that runs a Query from a QueryType, and assignsthe results to the view.
It supports mapping of viewed content/location properties to QueryTypeparameters.
Usage example
ezpublish: system: site: content_view: full:gallery: template: 'content/view/full/gallery.html.twig' controller: 'ez_query:locationQueryAction' params: query: query_type: app.location_children parameters: parentLocationId: @=location.id assign_results_to: images
The SearchResult object is assigned to the 'images' twig variable:
{% for searchResult in images.searchHits %} {{ render( controller( 'ez_content:viewAction',
6 . 2
{'content': searchResult.valueObject, 'viewType': 'embed'} ) }}{% endfor %}
6 . 3
Conclusion
And questions maybe ?
7 . 1
Custom Views
The View layer can be re-used in order to allow customization of reusablecomponents.
I. A value object to view (ex: SystemInfo)II. A View object (ex: SystemInfoView)III. A very short controller (ex: SystemInfoController)IV. A ViewBuilder (ex: SystemInfoViewBuilder)
7 . 2
The Value
Can be any value objectWill be made available in the view templatesLoaded from the ViewBuilder, using route parameters
7 . 3
The View object
Must implement the View interfaceMay implement the BaseView classInstantiated by the ViewBuilderMay implement other View interfaces to add Matchers supportExamples: ContentValueView, LocationValueView
7 . 4
The ViewBuilder
Must implement the ViewBuilder interfaceMust be registered with the ezpublish.view_builder.registryManual registration for now, using a compiler pass / DIC extensionUses the matched controller's name to filter requestsMust call a ViewConfigurator to apply templates & controllerconfiguration
7 . 5
Custom matchers
Allows custom business rules for view matching.
Example: View\Matcher\SystemInfo\Identifier.
Requires a custom ViewProvider and ViewMatcherFactory.
Examples: support_tools.view.system_info.provider andsupport_tools.view.matcher_factory in view.yml.
7 . 6
Optional: View configuration
Siteaccess aware, semantic view configuration.
Requirements:
a ConfigParser. Example: SystemInfoViewParserinjection into the ezpublish extension, if you want it below
7 . 7
Improvements possibilities
DX:matchersview providersview configuration
8
Conclusion & Questions
top related