Skip to content

Commit f78d181

Browse files
authored
Merge pull request #24 from kdambekalns/feature/category-page
FEATURE: List posts by category
2 parents 39a6614 + 65ae72b commit f78d181

File tree

3 files changed

+123
-19
lines changed

3 files changed

+123
-19
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace RobertLemke\Plugin\Blog\Eel;
6+
7+
use Neos\ContentRepository\Domain\Model\NodeInterface;
8+
use Neos\Eel\FlowQuery\FlowQuery;
9+
use Neos\Eel\FlowQuery\FlowQueryException;
10+
use Neos\Eel\FlowQuery\Operations\AbstractOperation;
11+
12+
/**
13+
* FlowQuery operation to filter by properties of type reference or references
14+
*/
15+
class FilterByReferenceOperation extends AbstractOperation {
16+
17+
/**
18+
* {@inheritdoc}
19+
*/
20+
protected static $shortName = 'filterByReference';
21+
22+
/**
23+
* {@inheritdoc}
24+
*/
25+
protected static $priority = 100;
26+
27+
/**
28+
* We can only handle CR Nodes.
29+
*
30+
* @param $context
31+
* @return bool
32+
*/
33+
public function canEvaluate($context) {
34+
return (!isset($context[0]) || ($context[0] instanceof NodeInterface));
35+
}
36+
37+
/**
38+
* First argument is property to filter by, must be of reference of references type.
39+
* Second is object to filter by, must be Node.
40+
*
41+
* @param FlowQuery $flowQuery
42+
* @param array $arguments The arguments for this operation.
43+
* @return void
44+
* @throws FlowQueryException
45+
* @throws \Neos\ContentRepository\Exception\NodeException
46+
*/
47+
public function evaluate(FlowQuery $flowQuery, array $arguments) {
48+
if (empty($arguments[0])) {
49+
throw new FlowQueryException('filterByReference() needs reference property name by which nodes should be filtered', 1545778273);
50+
}
51+
if (empty($arguments[1])) {
52+
throw new FlowQueryException('filterByReference() needs node reference by which nodes should be filtered', 1545778276);
53+
}
54+
55+
/** @var NodeInterface $nodeReference */
56+
list($filterByPropertyPath, $nodeReference) = $arguments;
57+
58+
$filteredNodes = [];
59+
foreach ($flowQuery->getContext() as $node) {
60+
/** @var NodeInterface $node */
61+
$propertyValue = $node->getProperty($filterByPropertyPath);
62+
if ($nodeReference === $propertyValue || (is_array($propertyValue) && in_array($nodeReference, $propertyValue, TRUE))) {
63+
$filteredNodes[] = $node;
64+
}
65+
}
66+
67+
$flowQuery->setContext($filteredNodes);
68+
}
69+
}

Resources/Private/Fusion/Root.fusion

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,26 @@ prototype(RobertLemke.Plugin.Blog:PostsOverview) < prototype(Neos.Neos:Content)
7373
}
7474
}
7575

76+
#
77+
# List posts in category, including pagination
78+
#
79+
prototype(RobertLemke.Plugin.Blog:Category) < prototype(Neos.NodeTypes:Page) {
80+
body.content.main = RobertLemke.Plugin.Blog:PostsOverview {
81+
postsNode >
82+
postNodes = ${q(site).find('[instanceof RobertLemke.Plugin.Blog:Post]').filterByReference('categories', documentNode).get()}
83+
}
84+
}
85+
86+
#
87+
# List posts in tag, including pagination
88+
#
89+
prototype(RobertLemke.Plugin.Blog:Tag) < prototype(Neos.NodeTypes:Page) {
90+
body.content.main = RobertLemke.Plugin.Blog:PostsOverview {
91+
postsNode >
92+
postNodes = ${q(site).find('[instanceof RobertLemke.Plugin.Blog:Post]').filterByReference('tags', documentNode).get()}
93+
}
94+
}
95+
7696
#
7797
# RSS feed
7898
#

Resources/Private/Templates/NodeTypes/PostsOverview.html

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,38 @@
2323
});
2424
</script>
2525
<div class="robertlemke-plugin-blog" id="scrolling-container">
26-
<cr:widget.paginate widgetId="posts-paginator" parentNode="{postsNode}" nodeTypeFilter="RobertLemke.Plugin.Blog:Post" as="paginatedPosts" configuration="{itemsPerPage: 5, insertBelow: 1, maximumNumberOfLinks: 15}">
27-
<ol class="posts">
28-
<f:for each="{paginatedPosts}" as="post">
29-
<li class="post">
30-
<h2>
31-
<neos:link.node node="{post}">{post.properties.title}</neos:link.node>
32-
<f:if condition="{post.removed}"> (removed)</f:if>
33-
</h2>
34-
<p class="content">
35-
<neos:link.node node="{post}" class="invisible-link">
36-
<blog:teaser node="{post}"/>
37-
</neos:link.node> <neos:link.node node="{post}" class="read-more"><f:translate package="RobertLemke.Plugin.Blog">Read more</f:translate></neos:link.node>
38-
</p>
39-
</li>
40-
<hr/>
41-
</f:for>
42-
</ol>
43-
{pagination.numberOfPages}
44-
</cr:widget.paginate>
26+
<f:if condition="{postsNode}"><f:render section="fromParentNode" arguments="{parentNode: postsNode}"/></f:if>
27+
<f:if condition="{postNodes}"><f:render section="fromNodes" arguments="{nodes: postNodes}"/></f:if>
4528
</div>
29+
30+
<f:section name="fromParentNode">
31+
<cr:widget.paginate widgetId="posts-paginator" parentNode="{parentNode}" nodeTypeFilter="RobertLemke.Plugin.Blog:Post" as="paginatedPosts" configuration="{itemsPerPage: 5, insertBelow: 1, maximumNumberOfLinks: 15}">
32+
<f:render section="paginatedPosts" arguments="{paginatedPosts: paginatedPosts}"/>
33+
</cr:widget.paginate>
34+
</f:section>
35+
36+
<f:section name="fromNodes">
37+
<cr:widget.paginate widgetId="posts-paginator" nodes="{nodes}" nodeTypeFilter="RobertLemke.Plugin.Blog:Post" as="paginatedPosts" configuration="{itemsPerPage: 5, insertBelow: 1, maximumNumberOfLinks: 15}">
38+
<f:render section="paginatedPosts" arguments="{paginatedPosts: paginatedPosts}"/>
39+
</cr:widget.paginate>
40+
</f:section>
41+
42+
<f:section name="paginatedPosts">
43+
<ol class="posts">
44+
<f:for each="{paginatedPosts}" as="post">
45+
<li class="post">
46+
<h2>
47+
<neos:link.node node="{post}">{post.properties.title}</neos:link.node>
48+
<f:if condition="{post.removed}"> (removed)</f:if>
49+
</h2>
50+
<p class="content">
51+
<neos:link.node node="{post}" class="invisible-link">
52+
<blog:teaser node="{post}"/>
53+
</neos:link.node> <neos:link.node node="{post}" class="read-more"><f:translate package="RobertLemke.Plugin.Blog">Read more</f:translate></neos:link.node>
54+
</p>
55+
</li>
56+
<hr/>
57+
</f:for>
58+
</ol>
59+
{pagination.numberOfPages}
60+
</f:section>

0 commit comments

Comments
 (0)