Skip to content

Commit 665a585

Browse files
author
Bertrand Dunogier
committed
Added Gallery images and Partner references examples
1 parent 07afa58 commit 665a585

File tree

4 files changed

+209
-0
lines changed

4 files changed

+209
-0
lines changed
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+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
### Gallery images query field
2+
A field that lists the images of a gallery.
3+
4+
#### Content type configuration
5+
The following assumes a "gallery" content type with an "images" query field.
6+
The actual images are sub-items of the gallery.
7+
8+
##### Query type
9+
AppBundle:Children
10+
11+
##### Returned type
12+
Image
13+
14+
##### Parameters
15+
```yaml
16+
parent_location_id: '@=mainLocation.id'
17+
included_content_type_identifier: '@=returnedType'
18+
```
19+
20+
#### Layout customization
21+
```yaml
22+
ezpublish:
23+
systems:
24+
site:
25+
content_view:
26+
# Customize the layout around all the images
27+
content_query_field:
28+
gallery_images:
29+
match:
30+
Identifier\ContentType: gallery
31+
Identifier\FieldDefinition: images
32+
template: "content/view/content_query_field/gallery_images.html.twig"
33+
# Customize the layout of each image
34+
line:
35+
images:
36+
match:
37+
Identifier\ContentType: image
38+
template: "content/view/line/images.html.twig"
39+
```
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
### Partners references query field
2+
A field that lists the references developed by a partner.
3+
4+
#### Content type configuration
5+
The "reference' content type with has a "partners" relation list field. Each partner involved in the reference is added.
6+
7+
The "partner" content type has a "references" content query field. It lists all references that include that partner.
8+
9+
##### Query type
10+
AppBundle:RelatedToContent
11+
12+
##### Returned type
13+
Reference
14+
15+
##### Parameters
16+
```yaml
17+
from_field: partners
18+
sort_by: Name
19+
content_type: '@=returnedType'
20+
to_content: '@=content'
21+
```
22+
23+
#### Layout customization
24+
```yaml
25+
ezpublish:
26+
systems:
27+
site:
28+
languages: [eng-GB]
29+
content_view:
30+
# Customize the layout around all the images
31+
content_query_field:
32+
partner_references:
33+
match:
34+
Identifier\ContentType: partner
35+
Identifier\FieldDefinition: references
36+
template: "content/view/content_query_field/partner_references.html.twig"
37+
# Customize the layout of each image
38+
line:
39+
reference:
40+
match:
41+
Identifier\ContentType: reference
42+
template: "content/view/line/reference.html.twig"
43+
```
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
/**
3+
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
4+
* @license For full copyright and license information view LICENSE file distributed with this source code.
5+
*/
6+
7+
namespace AppBundle\QueryType;
8+
9+
use eZ\Publish\API\Repository\Values\Content\Content;
10+
use eZ\Publish\API\Repository\Values\Content\Query;
11+
use eZ\Publish\Core\QueryType\OptionsResolverBasedQueryType;
12+
use Symfony\Component\OptionsResolver\OptionsResolver;
13+
14+
/**
15+
* For a given content, return items that have a relation to it using a specific field identifier.
16+
*
17+
* Parameters:
18+
* - Content to_content: the content item that is the target of the relations
19+
* - string from_field: the identifier of the field that has the relation
20+
* - string|string[] content_type (optional): a content type or set of content type to filter on
21+
* Default: name
22+
*
23+
* @todo add sort direction support
24+
*/
25+
class RelatedToContentQueryType extends OptionsResolverBasedQueryType
26+
{
27+
/**
28+
* @inheritDoc
29+
*/
30+
protected function configureOptions(OptionsResolver $optionsResolver)
31+
{
32+
$optionsResolver->setDefined(['to_content', 'from_field', 'content_type', 'sort_by']);
33+
$optionsResolver->setRequired(['to_content', 'from_field']);
34+
$optionsResolver->addAllowedTypes('to_content', Content::class);
35+
$optionsResolver->addAllowedTypes('from_field', 'string');
36+
$optionsResolver->addAllowedTypes('content_type', 'string');
37+
38+
}
39+
40+
/**
41+
* @inheritDoc
42+
*/
43+
protected function doGetQuery(array $parameters)
44+
{
45+
$query = new Query();
46+
$query->filter = new Query\Criterion\LogicalAnd([
47+
new Query\Criterion\FieldRelation($parameters['from_field'], Query\Criterion\Operator::CONTAINS, [$parameters['to_content']->id])
48+
]);
49+
if (isset($parameters['content_type'])) {
50+
$query->filter->criteria[] = new Query\Criterion\ContentTypeIdentifier($parameters['content_type']);
51+
}
52+
53+
if (isset($parameters['sort_by'])) {
54+
$sortClauseClass = 'Query\SortClause' . $parameters['sort_by'];
55+
if (class_exists($sortClauseClass)) {
56+
$query->sortClauses[] = new $sortClauseClass(Query::SORT_ASC);
57+
}
58+
}
59+
60+
if (empty($query->sortClauses)) {
61+
$query->sortClauses[] = new Query\SortClause\ContentName(Query::SORT_ASC);
62+
}
63+
64+
return $query;
65+
}
66+
67+
/**
68+
* @inheritDoc
69+
*/
70+
public static function getName()
71+
{
72+
return 'RelatedToContent';
73+
}
74+
}

0 commit comments

Comments
 (0)