Skip to content

Commit 012a1d4

Browse files
author
Bertrand Dunogier
committed
EZP-24264: applied QueryController #2 to blog
1 parent 99dc9ac commit 012a1d4

File tree

3 files changed

+112
-19
lines changed

3 files changed

+112
-19
lines changed

QueryType/BlogPostsQueryType.php

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
/**
3+
* @license For full copyright and license information view LICENSE file distributed with this source code.
4+
*/
5+
namespace EzSystems\DemoBundle\QueryType;
6+
7+
use eZ\Publish\API\Repository\Values\Content\Query;
8+
use eZ\Publish\Core\QueryType\OptionsResolverBasedQueryType;
9+
use eZ\Publish\API\Repository\Values\Content\Query\Criterion;
10+
use eZ\Publish\Core\QueryType\QueryType;
11+
use Symfony\Component\OptionsResolver\OptionsResolver;
12+
use eZ\Publish\API\Repository\Values\Content\Query\SortClause;
13+
14+
/**
15+
* A QueryType that lists the blog_post within a blog_post Location.
16+
*/
17+
class BlogPostsQueryType extends OptionsResolverBasedQueryType implements QueryType
18+
{
19+
/**
20+
* @var array
21+
*/
22+
private $languages;
23+
24+
/**
25+
* @param array $languages List of languages blog posts must be searched in.
26+
*/
27+
public function __construct(array $languages = [])
28+
{
29+
$this->languages = $languages;
30+
}
31+
32+
/**
33+
* Returns the QueryType name.
34+
* @return string
35+
*/
36+
public static function getName()
37+
{
38+
Return 'DemoBundle:BlogPosts';
39+
}
40+
41+
/**
42+
* Configures the OptionsResolver for the QueryType.
43+
* Example:
44+
* ```php
45+
* // type is required
46+
* $resolver->setRequired('type');
47+
* // limit is optional, and has a default value of 10
48+
* $resolver->setDefault('limit', 10);
49+
* ```
50+
*
51+
* @param OptionsResolver $optionsResolver
52+
*/
53+
protected function configureOptions(OptionsResolver $optionsResolver)
54+
{
55+
$optionsResolver->setRequired('blogPathString');
56+
57+
}
58+
59+
/**
60+
* Builds and returns the Query object.
61+
* The parameters array is processed with the OptionsResolver, meaning that it has been validated, and contains
62+
* the default values when applicable.
63+
*
64+
* @param array $parameters The QueryType parameters, pre-processed by the OptionsResolver
65+
*
66+
* @return Query
67+
*/
68+
protected function doGetQuery(array $parameters)
69+
{
70+
$languages = ['eng-GB'];
71+
72+
$criteria = [];
73+
$criteria[] = new Criterion\Subtree($parameters['blogPathString']);
74+
$criteria[] = new Criterion\ContentTypeIdentifier(array('blog_post'));
75+
$criteria[] = new Criterion\LanguageCode($languages);
76+
77+
$query = new Query();
78+
$query->query = new Criterion\LogicalAnd($criteria);
79+
$query->sortClauses = array(
80+
new SortClause\Field('blog_post', 'publication_date', Query::SORT_DESC, $languages[0]),
81+
);
82+
83+
return $query;
84+
}
85+
}

Resources/config/ezdemo.yml

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,6 @@ system:
2323
template: "eZDemoBundle:full:article.html.twig"
2424
match:
2525
Identifier\ContentType: [article]
26-
# There are two ways to add extra information to your response using a custom controller
27-
blog:
28-
# Fully customized, handling everything yourself
29-
controller: "eZDemoBundle:Demo:listBlogPosts"
30-
match:
31-
Identifier\ContentType: [blog]
3226
blog_post:
3327
# Enriched controller, only adding extra parameters
3428
controller: "eZDemoBundle:Demo:showBlogPost"
@@ -80,11 +74,6 @@ system:
8074
template: "eZDemoBundle:line:article.html.twig"
8175
match:
8276
Identifier\ContentType: [article]
83-
blog_post:
84-
controller: "eZDemoBundle:Demo:showBlogPost"
85-
template: "eZDemoBundle:line:blog_post.html.twig"
86-
match:
87-
Identifier\ContentType: [blog_post]
8877
place:
8978
template: "eZDemoBundle:line:place.html.twig"
9079
match:
@@ -107,6 +96,19 @@ system:
10796
Identifier\ContentType: [video]
10897

10998
content_view:
99+
full:
100+
# Fully customized, handling everything yourself
101+
blog:
102+
match:
103+
Identifier\ContentType: [blog]
104+
controller: 'ez_query:contentAction'
105+
template: 'eZDemoBundle:full:blog.html.twig'
106+
params:
107+
query: 'DemoBundle:BlogPosts'
108+
queryParameters:
109+
blogPathString: @=location.pathString
110+
variable: blog_posts_list
111+
enablePager: true
110112
embed:
111113
image:
112114
template: "eZDemoBundle:embed:image.html.twig"
@@ -117,6 +119,12 @@ system:
117119
template: "eZDemoBundle:relation:image.html.twig"
118120
match:
119121
Identifier\ContentType: [image]
122+
line:
123+
blog_post:
124+
controller: "eZDemoBundle:Demo:showBlogPost"
125+
template: "eZDemoBundle:line:blog_post.html.twig"
126+
match:
127+
Identifier\ContentType: [blog_post]
120128

121129
field_templates:
122130
- {template: "eZDemoBundle::content_fields.html.twig", priority: 10}

Resources/views/full/blog.html.twig

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,20 @@
55
<div class="class-blog">
66
<div class="row">
77
<div class="col-md-8">
8-
{% if pagerBlog|length() > 0 %}
8+
{% if blog_posts_list|length() > 0 %}
99
<section class="content-view-children">
10-
{% for post in pagerBlog %}
10+
{% for post in blog_posts_list.searchHits %}
1111
{# Displaying blog_post elements calling the view line #}
12-
{{ render_esi( controller( 'ez_content:viewLocation', {'locationId': post.contentInfo.mainLocationId, 'viewType': 'line'} ) ) }}
12+
{{ render_esi( controller( 'ez_content:viewLocation', {'locationId': post.valueObject.contentInfo.mainLocationId, 'viewType': 'line'} ) ) }}
1313
{% endfor %}
1414
</section>
1515

1616
{# Pagination is displayed only if needed (number of posts > limit) #}
17-
{% if pagerBlog.haveToPaginate() %}
18-
<div class="pagination-centered">
19-
{{ pagerfanta( pagerBlog, 'twitter_bootstrap_translated', {'routeName': location} ) }}
20-
</div>
21-
{% endif %}
17+
{#{% if blog_posts_list.haveToPaginate() %}#}
18+
{#<div class="pagination-centered">#}
19+
{#{{ pagerfanta( blog_posts_list, 'twitter_bootstrap_translated', {'routeName': location} ) }}#}
20+
{#</div>#}
21+
{#{% endif %}#}
2222

2323
{% endif %}
2424
</div>

0 commit comments

Comments
 (0)