Skip to content

Commit eb2c16c

Browse files
author
Bertrand Dunogier
committed
Completed examples
Unified documentation, added new ones, made them more generic.
1 parent 5278f5f commit eb2c16c

File tree

12 files changed

+337
-6
lines changed

12 files changed

+337
-6
lines changed

doc/examples/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
The examples in this directory are meant to present different usages for the query field.
2+
They all come with their own query type, as well as a README file that guides you into using the example.
3+
4+
## List of examples
5+
6+
### `children`
7+
[documentation](children/README.md)
8+
9+
Children of a content item: images of a gallery, most recent articles of a category...
10+
11+
### `map_location_distance`
12+
[documentation](map_location_distance/README.md)
13+
14+
Items located within a given distance of an item's geographical location.
15+
16+
### `reverse_relations`
17+
[documentation](reverse_relations/README.md)
18+
19+
Items related to the current item.
20+
21+
### `ancestors`
22+
[documentation](children/README.md)
23+
24+
Ancestors of a type.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
/**
4+
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
5+
* @license For full copyright and license information view LICENSE file distributed with this source code.
6+
*/
7+
namespace App\QueryType;
8+
9+
use eZ\Publish\API\Repository\LocationService;
10+
use eZ\Publish\API\Repository\Values\Content\Content;
11+
use eZ\Publish\API\Repository\Values\Content\Location;
12+
use eZ\Publish\API\Repository\Values\Content\Query;
13+
use eZ\Publish\API\Repository\Values\Content\Query\Criterion;
14+
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException;
15+
use eZ\Publish\Core\QueryType\QueryType;
16+
17+
class AncestorsOfContentQueryType implements QueryType
18+
{
19+
/**
20+
* @var \eZ\Publish\API\Repository\LocationService
21+
*/
22+
private $locationService;
23+
24+
public function __construct(LocationService $locationService)
25+
{
26+
$this->locationService = $locationService;
27+
}
28+
29+
public function getQuery(array $parameters = [])
30+
{
31+
if ($parameters['content'] instanceof Content) {
32+
$pathStrings = array_map(
33+
function (Location $location) {
34+
return $location->pathString;
35+
},
36+
$this->locationService->loadLocations($parameters['content']->contentInfo)
37+
);
38+
} else {
39+
throw new InvalidArgumentException('content', 'should be of type API\Content');
40+
}
41+
42+
$filter = new Criterion\LogicalAnd([
43+
new Criterion\Ancestor($pathStrings),
44+
new Criterion\ContentTypeIdentifier($parameters['type']),
45+
]);
46+
47+
return new Query([
48+
'filter' => $filter,
49+
]);
50+
}
51+
52+
public function getSupportedParameters()
53+
{
54+
return ['content', 'type'];
55+
}
56+
57+
public static function getName()
58+
{
59+
return 'AncestorsOfContent';
60+
}
61+
}

doc/examples/ancestors/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Query field: ancestors example
2+
A field that, given a content item, returns the ancestors of that item's locations that are of a given type. Example use-case:
3+
4+
## Example
5+
Images are added as children of Gallery items.
6+
The `images.gallery` field returns the galleries any image is part of.
7+
8+
### Query Type
9+
`AncestorsOfContent`
10+
11+
### Returned type
12+
Image
13+
14+
### Parameters
15+
```yaml
16+
content: "@=content"
17+
type: "@=returnedType"
18+
```
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace AppBundle\QueryType;
4+
5+
use eZ\Publish\Core\QueryType\QueryType;
6+
use eZ\Publish\API\Repository\Values\Content\Query;
7+
8+
class ChildrenQueryType implements QueryType
9+
{
10+
public function getQuery(array $parameters = [])
11+
{
12+
$options = [];
13+
14+
$criteria = [
15+
new Query\Criterion\Visibility(Query\Criterion\Visibility::VISIBLE),
16+
];
17+
18+
if (!empty($parameters['parent_location_id'])) {
19+
$criteria[] = new Query\Criterion\ParentLocationId($parameters['parent_location_id']);
20+
} else {
21+
$criteria[] = new Query\Criterion\MatchNone();
22+
}
23+
24+
if (!empty($parameters['included_content_type_identifier'])) {
25+
$criteria[] = new Query\Criterion\ContentTypeIdentifier($parameters['included_content_type_identifier']);
26+
}
27+
28+
$options['filter'] = new Query\Criterion\LogicalAnd($criteria);
29+
30+
if (isset($parameters['limit'])) {
31+
$options['limit'] = $parameters['limit'];
32+
}
33+
34+
$options['sortClauses'] = [new Query\SortClause\DatePublished(Query::SORT_DESC)];
35+
36+
return new Query($options);
37+
}
38+
39+
public static function getName()
40+
{
41+
return 'AppBundle:Children';
42+
}
43+
44+
public function getSupportedParameters()
45+
{
46+
return [
47+
'parent_location_id',
48+
'included_content_type_identifier',
49+
'limit',
50+
'whatever'
51+
];
52+
}
53+
}

doc/examples/children/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
### Gallery images query field
2+
A field that lists the children of a given type below the current location.
3+
4+
#### Content type configuration
5+
The following assumes a "gallery" content type with an "images" query field.
6+
The images are the sub-items of the gallery.
7+
8+
9+
##### Query type
10+
`AppBundle:Children` (defined in `ChildrenQueryType.php`)
11+
12+
##### Returned type
13+
Image
14+
15+
##### Parameters
16+
```yaml
17+
parent_location_id: '@=mainLocation.id'
18+
included_content_type_identifier: '@=returnedType'
19+
```
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<div class="row text-center text-lg-left">
2+
{% for item in items %}
3+
{{ render(controller("ez_content:viewAction", {
4+
"contentId": item.id,
5+
"viewType": itemViewType
6+
})) }}
7+
{% endfor %}
8+
</div>
9+
10+
{% if isPaginationEnabled %}
11+
{{ pagerfanta( items, 'ez', {'routeName': location, 'pageParameter': pageParameter } ) }}
12+
{% endif %}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<div class="col-lg-3 col-md-4 col-6">
2+
{{ ez_render_field(
3+
content, 'image',
4+
{
5+
parameters: {
6+
alias: 'small',
7+
attrs: {class: 'img-fluid img-thumbnail'}
8+
}
9+
}
10+
) }}
11+
</div>

doc/examples/children/views.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
ezpublish:
2+
systems:
3+
site:
4+
content_view:
5+
# Customize the layout around all the images
6+
content_query_field:
7+
gallery_images:
8+
match:
9+
Identifier\ContentType: gallery
10+
Identifier\FieldDefinition: images
11+
template: "content/view/content_query_field/gallery_images.html.twig"
12+
# Customize the layout of each image
13+
line:
14+
images:
15+
match:
16+
Identifier\ContentType: image
17+
template: "content/view/line/images.html.twig"

doc/examples/nearby_places/README.md renamed to doc/examples/map_location_distance/README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
### Nearby places query field
2-
3-
A field that lists Place items located close to the current item.
2+
A field that returns items based on their distance relative to the current item.
43

54
#### Content type configuration
65
The following assumes a "place" content item with a "location" map location
76
field definition.
87

98
##### Query type
10-
"Nearby places" (see [NearbyPlacesQueryType](NearbyPlacesQueryType.php).
9+
"RelativeDistance" (see [NearbyPlacesQueryType](NearbyPlacesQueryType.php).
1110

1211
##### Parameters
1312
```yaml

doc/examples/nearby_places/NearbyPlacesQueryType.php renamed to doc/examples/map_location_distance/RelativeDistanceQueryType.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
55
* @license For full copyright and license information view LICENSE file distributed with this source code.
66
*/
7-
namespace App\QueryType;
7+
namespace AppBundle\QueryType;
88

99
use eZ\Publish\API\Repository\Values\Content\Query;
1010
use eZ\Publish\API\Repository\Values\Content\Query\Criterion;
1111
use eZ\Publish\Core\QueryType\QueryType;
1212

13-
class NearbyPlacesQueryType implements QueryType
13+
class RelativeDistanceQueryType implements QueryType
1414
{
1515
public function getQuery(array $parameters = [])
1616
{
@@ -35,6 +35,6 @@ public function getSupportedParameters()
3535

3636
public static function getName()
3737
{
38-
return 'NearbyPlaces';
38+
return 'RelativeDistance';
3939
}
4040
}

0 commit comments

Comments
 (0)