Skip to content

Commit

Permalink
Fix lolautruche#39: Configurable view don't have current content/loca…
Browse files Browse the repository at this point in the history
…tion any more
  • Loading branch information
lolautruche committed Aug 28, 2017
1 parent cec1bff commit 8f172ac
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 5 deletions.
24 changes: 23 additions & 1 deletion EventListener/ViewTemplateListener.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
4 changes: 2 additions & 2 deletions Resources/doc/template_variables_injection.md
Expand Up @@ -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'];
Expand Down
60 changes: 60 additions & 0 deletions 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,
);
}
}}
```
30 changes: 28 additions & 2 deletions View/ConfigurableView.php
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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;
}
}

0 comments on commit 8f172ac

Please sign in to comment.