Skip to content

Commit

Permalink
EZP-24264: QueryController ExpressionLanguage support
Browse files Browse the repository at this point in the history
Entries from params.queryParameter will be searched for values starting
with '@=' (standard expression language syntax in yaml).

The expressions will be processed by the QueryController, and have access
to:
- `view`: the ContentView
- `content`: the rendered Content
- `location`: the rendered Location, if any

This expression would pass the location id as the parentLocationId queryParameter:
```
params:
  queryParameters:
    parentLocationId: @=location.id
```
  • Loading branch information
Bertrand Dunogier committed Oct 17, 2015
1 parent 10d7b96 commit 84d6c9f
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions eZ/Publish/Core/MVC/Symfony/Controller/Content/QueryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException;
use eZ\Publish\Core\MVC\Symfony\View\ContentView;
use eZ\Publish\Core\QueryType\QueryTypeRegistry;
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;

class QueryController
{
Expand Down Expand Up @@ -46,7 +47,7 @@ public function contentInfoAction(ContentView $view)
}

/**
* Returns the query configured in the view parameters
* Returns the query configured in the view parameters.
*
* @param \eZ\Publish\Core\MVC\Symfony\View\ContentView $view
*
Expand All @@ -58,17 +59,23 @@ private function getQuery(ContentView $view)
if (!$view->hasParameter('query')) {
throw new InvalidArgumentException('query', 'Missing required query parameter');
}
$queryType = $this->queryTypeRegistry->getQueryType($view->getParameter('queryType'));
$queryType = $this->queryTypeRegistry->getQueryType($view->getParameter('query'));

$queryParameters = $view->hasParameter('queryParameters') ? $view->getParameter('query_parameters') : [];
$queryParameters = $view->hasParameter('queryParameters') ? $view->getParameter('queryParameters') : [];
$supportedQueryParameters = array_flip($queryType->getSupportedParameters());
foreach (array_keys($queryParameters) as $queryParameterName) {
if (!isset( $supportedQueryParameters[$queryParameterName] )) {
throw new InvalidArgumentException('query parameters',
"unsupported query parameter $queryParameterName");
foreach ($queryParameters as $queryParameterName => $queryParameterValue) {
if (!isset($supportedQueryParameters[$queryParameterName])) {
throw new InvalidArgumentException("parameter $queryParameterName", 'unsupported query parameter');
}
if (substr($queryParameterValue, 0, 2) == '@=') {
$language = new ExpressionLanguage();
$queryParameters[$queryParameterName] = $language->evaluate(
substr($queryParameterValue, 2),
['view' => $view, 'location' => $view->getLocation(), 'content' => $view->getContent()]
);
}
}

return $queryType->getQuery($queryParameters);
}
}
}

0 comments on commit 84d6c9f

Please sign in to comment.