diff --git a/EventListener/ViewTemplateListener.php b/EventListener/ViewTemplateListener.php index 0b021c7..fd79e76 100644 --- a/EventListener/ViewTemplateListener.php +++ b/EventListener/ViewTemplateListener.php @@ -15,6 +15,9 @@ use eZ\Publish\Core\MVC\ConfigResolverInterface; use eZ\Publish\Core\MVC\Symfony\Event\PreContentViewEvent; use eZ\Publish\Core\MVC\Symfony\MVCEvents; +use eZ\Publish\Core\MVC\Symfony\View\ContentValueView; +use eZ\Publish\Core\MVC\Symfony\View\LocationValueView; +use eZ\Publish\Core\MVC\Symfony\View\View; use Lolautruche\EzCoreExtraBundle\Exception\MissingParameterProviderException; use Lolautruche\EzCoreExtraBundle\View\ConfigurableView; use Lolautruche\EzCoreExtraBundle\View\ViewParameterProviderInterface; @@ -93,10 +96,29 @@ public function onPreContentView(PreContentViewEvent $event) // The resulted array is casted to object (stdClass) for convenient use in templates. // Parameter name will be unchanged. Parameters returned by provider will then be "namespaced" by the parameter name. $provider = $this->parameterProviders[$param['provider']]; - $param = (object) $provider->getViewParameters(new ConfigurableView($view), $paramProviderOptions); + $param = (object) $provider->getViewParameters($this->generateConfigurableView($view), $paramProviderOptions); } } $view->setParameters(array_replace($view->getParameters(), $configHash['params'])); } + + /** + * @param View $view + * @return ConfigurableView + */ + private function generateConfigurableView(View $view) + { + $configurableView = new ConfigurableView($view); + $params = []; + if ($view instanceof ContentValueView) { + $params['content'] = $view->getContent(); + } + if ($view instanceof LocationValueView) { + $params['location'] = $view->getLocation(); + } + $configurableView->addParameters($params); + + return $configurableView; + } } diff --git a/Resources/doc/template_variables_injection.md b/Resources/doc/template_variables_injection.md index 5730310..aefb040 100644 --- a/Resources/doc/template_variables_injection.md +++ b/Resources/doc/template_variables_injection.md @@ -169,8 +169,8 @@ class MyViewParameterProvider extends ConfigurableViewParameterProvider protected function doGetParameters(ConfigurableView $view, array $options = []) { // Current location and content are available in content/location views - $location = $view->getParameter('location'); - $content = $view->getParameter('content'); + $location = $view->getLocation(); + $content = $view->getContent(); // Passed options $contentTypeForChildren = $options['children_type']; diff --git a/UPGRADE-3.0.md b/UPGRADE-3.0.md new file mode 100644 index 0000000..670448e --- /dev/null +++ b/UPGRADE-3.0.md @@ -0,0 +1,60 @@ +# UPGRADE FROM 2.x to 3.0 + +## Template variables injection +### View parameter providers + +#### Access to `content` and `location` +Access to current `Content` and `Location` objects have changed. +They're now available via `ConfigurableView::getContent()` and `ConfigurableView::getLocation`. + +**Before** + +```php +class MyParamProvider +{ + public function getParameters(ConfigurableView $view, array $options = []) + { + // Current location and content are available in content/location views + $location = $view->getParameter('content'); + $content = $view->getParameter('location'); + + // Passed options + $contentTypeForChildren = $options['children_type']; + $childrenLimit = $options['children_limit']; + // Fetch children with those options + // $fetchedChildren = ... + + return array( + 'foo' => $this->someService->giveMeFoo(), + 'some' => 'thing', + 'children' => $fetchedChildren, + ); + } +}} +``` + +**After** + +```php +class MyParamProvider +{ + public function getParameters(ConfigurableView $view, array $options = []) + { + // Current location and content are available in content/location views + $location = $view->getContent(); + $content = $view->getLocation(); + + // Passed options + $contentTypeForChildren = $options['children_type']; + $childrenLimit = $options['children_limit']; + // Fetch children with those options + // $fetchedChildren = ... + + return array( + 'foo' => $this->someService->giveMeFoo(), + 'some' => 'thing', + 'children' => $fetchedChildren, + ); + } +}} +``` diff --git a/View/ConfigurableView.php b/View/ConfigurableView.php index 769ff99..2665e54 100644 --- a/View/ConfigurableView.php +++ b/View/ConfigurableView.php @@ -9,6 +9,8 @@ namespace Lolautruche\EzCoreExtraBundle\View; +use eZ\Publish\Core\MVC\Symfony\View\ContentValueView; +use eZ\Publish\Core\MVC\Symfony\View\LocationValueView; use eZ\Publish\Core\MVC\Symfony\View\View; use Lolautruche\EzCoreExtraBundle\Exception\UnsupportedException; use Symfony\Component\HttpFoundation\Response; @@ -18,10 +20,10 @@ * Decoration of original view that can be used with view parameter providers. * It is basically only possible to add new parameters and access to original view parameters. */ -class ConfigurableView implements View +class ConfigurableView implements View, ContentValueView, LocationValueView { /** - * @var \eZ\Publish\Core\MVC\Symfony\View\View + * @var \eZ\Publish\Core\MVC\Symfony\View\View|ContentValueView|LocationValueView */ private $innerView; @@ -103,6 +105,12 @@ public function getParameter($parameterName) return $this->parameters[$parameterName]; } + if ($parameterName === 'content') { + @trigger_error('Access to current content via getParameter() is deprecated. Use getContent() instead.', E_USER_DEPRECATED); + } elseif ($parameterName === 'location') { + @trigger_error('Access to current location via getParameter() is deprecated. Use getLocation() instead.', E_USER_DEPRECATED); + } + return $this->innerView->getParameter($parameterName); } @@ -150,4 +158,22 @@ public function getResponse() { return $this->innerView->getResponse(); } + + /** + * Returns the Content. + * + * @return \eZ\Publish\API\Repository\Values\Content\Content + */ + public function getContent() + { + return $this->innerView instanceof ContentValueView ? $this->innerView->getContent() : null; + } + + /** + * @return \eZ\Publish\API\Repository\Values\Content\Location + */ + public function getLocation() + { + return $this->innerView instanceof LocationValueView ? $this->innerView->getContent() : null; + } }