27
1 The eZ Platform View Layer Bertrand Dunogier Platform Lead [email protected] @bdunogier This presentation is online: bdunogier.github.io/talks/ezconf2016-ezplatform-views

The eZ Platform view layer – eZ Conference 2016

Embed Size (px)

Citation preview

Page 1: The eZ Platform view layer – eZ Conference 2016

1

The eZ PlatformView Layer

Bertrand Dunogier Platform Lead [email protected] @bdunogier

This presentation is online: bdunogier.github.io/talks/ezconf2016-ezplatform-views

Page 2: The eZ Platform view layer – eZ Conference 2016

2

About me

11th year at eZ, 13th year around itPlatform backend lead engineerFocused on REST & Symfony layers

Page 3: The eZ Platform view layer – eZ Conference 2016

3

Plan

I. The View LayerII. Changes in Platform 1.xIII. Query typesIV. The Query ControllerV. Using the View layer in your projects

Page 4: The eZ Platform view layer – eZ Conference 2016

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'

Page 5: The eZ Platform view layer – eZ Conference 2016

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 );}

Page 6: The eZ Platform view layer – eZ Conference 2016

4 . 3

Drawbacks

Requires complex callsError proneTight coupling with the extended view controller, complex BC

Page 7: The eZ Platform view layer – eZ Conference 2016

4 . 4

Page 8: The eZ Platform view layer – eZ Conference 2016

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.

Page 9: The eZ Platform view layer – eZ Conference 2016

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);

Page 10: The eZ Platform view layer – eZ Conference 2016

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);

Page 11: The eZ Platform view layer – eZ Conference 2016

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}

Page 12: The eZ Platform view layer – eZ Conference 2016

5 . 4

The getSupportedParameters() method

Returns the supported parameters names.

public function getSupportedParameters(){ return ['parentLocationId', 'offset', 'limit'];}

Page 13: The eZ Platform view layer – eZ Conference 2016

5 . 5

The getName static method

Returns the name of the QueryType.public function getName(){ return 'app.location_children';}

(Thank you Mr. Obvious)

Page 14: The eZ Platform view layer – eZ Conference 2016

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, ]);}

Page 15: The eZ Platform view layer – eZ Conference 2016

5 . 7

Using QueryTypes from PHP

inject a QueryType using its serviceuse the ezpublish.query_type.registry service

Page 16: The eZ Platform view layer – eZ Conference 2016

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.

Page 17: The eZ Platform view layer – eZ Conference 2016

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',

Page 18: The eZ Platform view layer – eZ Conference 2016

6 . 2

{'content': searchResult.valueObject, 'viewType': 'embed'} ) }}{% endfor %}

Page 19: The eZ Platform view layer – eZ Conference 2016

6 . 3

Conclusion

And questions maybe ?

Page 20: The eZ Platform view layer – eZ Conference 2016

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)

Page 21: The eZ Platform view layer – eZ Conference 2016

7 . 2

The Value

Can be any value objectWill be made available in the view templatesLoaded from the ViewBuilder, using route parameters

Page 22: The eZ Platform view layer – eZ Conference 2016

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

Page 23: The eZ Platform view layer – eZ Conference 2016

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

Page 24: The eZ Platform view layer – eZ Conference 2016

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.

Page 25: The eZ Platform view layer – eZ Conference 2016

7 . 6

Optional: View configuration

Siteaccess aware, semantic view configuration.

Requirements:

a ConfigParser. Example: SystemInfoViewParserinjection into the ezpublish extension, if you want it below

Page 26: The eZ Platform view layer – eZ Conference 2016

7 . 7

Improvements possibilities

DX:matchersview providersview configuration

Page 27: The eZ Platform view layer – eZ Conference 2016

8

Conclusion & Questions