From 012a1d466f6ced4792b3a03ef6a07b185a462b9a Mon Sep 17 00:00:00 2001 From: Bertrand Dunogier Date: Sat, 17 Oct 2015 22:20:42 +0200 Subject: [PATCH] EZP-24264: applied QueryController #2 to blog --- QueryType/BlogPostsQueryType.php | 85 +++++++++++++++++++++++++++++ Resources/config/ezdemo.yml | 30 ++++++---- Resources/views/full/blog.html.twig | 16 +++--- 3 files changed, 112 insertions(+), 19 deletions(-) create mode 100644 QueryType/BlogPostsQueryType.php diff --git a/QueryType/BlogPostsQueryType.php b/QueryType/BlogPostsQueryType.php new file mode 100644 index 0000000..138a297 --- /dev/null +++ b/QueryType/BlogPostsQueryType.php @@ -0,0 +1,85 @@ +languages = $languages; + } + + /** + * Returns the QueryType name. + * @return string + */ + public static function getName() + { + Return 'DemoBundle:BlogPosts'; + } + + /** + * Configures the OptionsResolver for the QueryType. + * Example: + * ```php + * // type is required + * $resolver->setRequired('type'); + * // limit is optional, and has a default value of 10 + * $resolver->setDefault('limit', 10); + * ``` + * + * @param OptionsResolver $optionsResolver + */ + protected function configureOptions(OptionsResolver $optionsResolver) + { + $optionsResolver->setRequired('blogPathString'); + + } + + /** + * Builds and returns the Query object. + * The parameters array is processed with the OptionsResolver, meaning that it has been validated, and contains + * the default values when applicable. + * + * @param array $parameters The QueryType parameters, pre-processed by the OptionsResolver + * + * @return Query + */ + protected function doGetQuery(array $parameters) + { + $languages = ['eng-GB']; + + $criteria = []; + $criteria[] = new Criterion\Subtree($parameters['blogPathString']); + $criteria[] = new Criterion\ContentTypeIdentifier(array('blog_post')); + $criteria[] = new Criterion\LanguageCode($languages); + + $query = new Query(); + $query->query = new Criterion\LogicalAnd($criteria); + $query->sortClauses = array( + new SortClause\Field('blog_post', 'publication_date', Query::SORT_DESC, $languages[0]), + ); + + return $query; + } +} diff --git a/Resources/config/ezdemo.yml b/Resources/config/ezdemo.yml index 6533b65..ebe2fc8 100644 --- a/Resources/config/ezdemo.yml +++ b/Resources/config/ezdemo.yml @@ -23,12 +23,6 @@ system: template: "eZDemoBundle:full:article.html.twig" match: Identifier\ContentType: [article] - # There are two ways to add extra information to your response using a custom controller - blog: - # Fully customized, handling everything yourself - controller: "eZDemoBundle:Demo:listBlogPosts" - match: - Identifier\ContentType: [blog] blog_post: # Enriched controller, only adding extra parameters controller: "eZDemoBundle:Demo:showBlogPost" @@ -80,11 +74,6 @@ system: template: "eZDemoBundle:line:article.html.twig" match: Identifier\ContentType: [article] - blog_post: - controller: "eZDemoBundle:Demo:showBlogPost" - template: "eZDemoBundle:line:blog_post.html.twig" - match: - Identifier\ContentType: [blog_post] place: template: "eZDemoBundle:line:place.html.twig" match: @@ -107,6 +96,19 @@ system: Identifier\ContentType: [video] content_view: + full: + # Fully customized, handling everything yourself + blog: + match: + Identifier\ContentType: [blog] + controller: 'ez_query:contentAction' + template: 'eZDemoBundle:full:blog.html.twig' + params: + query: 'DemoBundle:BlogPosts' + queryParameters: + blogPathString: @=location.pathString + variable: blog_posts_list + enablePager: true embed: image: template: "eZDemoBundle:embed:image.html.twig" @@ -117,6 +119,12 @@ system: template: "eZDemoBundle:relation:image.html.twig" match: Identifier\ContentType: [image] + line: + blog_post: + controller: "eZDemoBundle:Demo:showBlogPost" + template: "eZDemoBundle:line:blog_post.html.twig" + match: + Identifier\ContentType: [blog_post] field_templates: - {template: "eZDemoBundle::content_fields.html.twig", priority: 10} diff --git a/Resources/views/full/blog.html.twig b/Resources/views/full/blog.html.twig index 0333283..c0ec0bb 100644 --- a/Resources/views/full/blog.html.twig +++ b/Resources/views/full/blog.html.twig @@ -5,20 +5,20 @@
- {% if pagerBlog|length() > 0 %} + {% if blog_posts_list|length() > 0 %}
- {% for post in pagerBlog %} + {% for post in blog_posts_list.searchHits %} {# Displaying blog_post elements calling the view line #} - {{ render_esi( controller( 'ez_content:viewLocation', {'locationId': post.contentInfo.mainLocationId, 'viewType': 'line'} ) ) }} + {{ render_esi( controller( 'ez_content:viewLocation', {'locationId': post.valueObject.contentInfo.mainLocationId, 'viewType': 'line'} ) ) }} {% endfor %}
{# Pagination is displayed only if needed (number of posts > limit) #} - {% if pagerBlog.haveToPaginate() %} -
- {{ pagerfanta( pagerBlog, 'twitter_bootstrap_translated', {'routeName': location} ) }} -
- {% endif %} + {#{% if blog_posts_list.haveToPaginate() %}#} + {#
#} + {#{{ pagerfanta( blog_posts_list, 'twitter_bootstrap_translated', {'routeName': location} ) }}#} + {#
#} + {#{% endif %}#} {% endif %}