Skip to content

Commit

Permalink
Merge pull request #24 from kdambekalns/feature/category-page
Browse files Browse the repository at this point in the history
FEATURE: List posts by category
  • Loading branch information
robertlemke authored Feb 21, 2019
2 parents 39a6614 + 65ae72b commit f78d181
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 19 deletions.
69 changes: 69 additions & 0 deletions Classes/Eel/FilterByReferenceOperation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

declare(strict_types=1);

namespace RobertLemke\Plugin\Blog\Eel;

use Neos\ContentRepository\Domain\Model\NodeInterface;
use Neos\Eel\FlowQuery\FlowQuery;
use Neos\Eel\FlowQuery\FlowQueryException;
use Neos\Eel\FlowQuery\Operations\AbstractOperation;

/**
* FlowQuery operation to filter by properties of type reference or references
*/
class FilterByReferenceOperation extends AbstractOperation {

/**
* {@inheritdoc}
*/
protected static $shortName = 'filterByReference';

/**
* {@inheritdoc}
*/
protected static $priority = 100;

/**
* We can only handle CR Nodes.
*
* @param $context
* @return bool
*/
public function canEvaluate($context) {
return (!isset($context[0]) || ($context[0] instanceof NodeInterface));
}

/**
* First argument is property to filter by, must be of reference of references type.
* Second is object to filter by, must be Node.
*
* @param FlowQuery $flowQuery
* @param array $arguments The arguments for this operation.
* @return void
* @throws FlowQueryException
* @throws \Neos\ContentRepository\Exception\NodeException
*/
public function evaluate(FlowQuery $flowQuery, array $arguments) {
if (empty($arguments[0])) {
throw new FlowQueryException('filterByReference() needs reference property name by which nodes should be filtered', 1545778273);
}
if (empty($arguments[1])) {
throw new FlowQueryException('filterByReference() needs node reference by which nodes should be filtered', 1545778276);
}

/** @var NodeInterface $nodeReference */
list($filterByPropertyPath, $nodeReference) = $arguments;

$filteredNodes = [];
foreach ($flowQuery->getContext() as $node) {
/** @var NodeInterface $node */
$propertyValue = $node->getProperty($filterByPropertyPath);
if ($nodeReference === $propertyValue || (is_array($propertyValue) && in_array($nodeReference, $propertyValue, TRUE))) {
$filteredNodes[] = $node;
}
}

$flowQuery->setContext($filteredNodes);
}
}
20 changes: 20 additions & 0 deletions Resources/Private/Fusion/Root.fusion
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,26 @@ prototype(RobertLemke.Plugin.Blog:PostsOverview) < prototype(Neos.Neos:Content)
}
}

#
# List posts in category, including pagination
#
prototype(RobertLemke.Plugin.Blog:Category) < prototype(Neos.NodeTypes:Page) {
body.content.main = RobertLemke.Plugin.Blog:PostsOverview {
postsNode >
postNodes = ${q(site).find('[instanceof RobertLemke.Plugin.Blog:Post]').filterByReference('categories', documentNode).get()}
}
}

#
# List posts in tag, including pagination
#
prototype(RobertLemke.Plugin.Blog:Tag) < prototype(Neos.NodeTypes:Page) {
body.content.main = RobertLemke.Plugin.Blog:PostsOverview {
postsNode >
postNodes = ${q(site).find('[instanceof RobertLemke.Plugin.Blog:Post]').filterByReference('tags', documentNode).get()}
}
}

#
# RSS feed
#
Expand Down
53 changes: 34 additions & 19 deletions Resources/Private/Templates/NodeTypes/PostsOverview.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,38 @@
});
</script>
<div class="robertlemke-plugin-blog" id="scrolling-container">
<cr:widget.paginate widgetId="posts-paginator" parentNode="{postsNode}" nodeTypeFilter="RobertLemke.Plugin.Blog:Post" as="paginatedPosts" configuration="{itemsPerPage: 5, insertBelow: 1, maximumNumberOfLinks: 15}">
<ol class="posts">
<f:for each="{paginatedPosts}" as="post">
<li class="post">
<h2>
<neos:link.node node="{post}">{post.properties.title}</neos:link.node>
<f:if condition="{post.removed}"> (removed)</f:if>
</h2>
<p class="content">
<neos:link.node node="{post}" class="invisible-link">
<blog:teaser node="{post}"/>
</neos:link.node> <neos:link.node node="{post}" class="read-more"><f:translate package="RobertLemke.Plugin.Blog">Read more</f:translate></neos:link.node>
</p>
</li>
<hr/>
</f:for>
</ol>
{pagination.numberOfPages}
</cr:widget.paginate>
<f:if condition="{postsNode}"><f:render section="fromParentNode" arguments="{parentNode: postsNode}"/></f:if>
<f:if condition="{postNodes}"><f:render section="fromNodes" arguments="{nodes: postNodes}"/></f:if>
</div>

<f:section name="fromParentNode">
<cr:widget.paginate widgetId="posts-paginator" parentNode="{parentNode}" nodeTypeFilter="RobertLemke.Plugin.Blog:Post" as="paginatedPosts" configuration="{itemsPerPage: 5, insertBelow: 1, maximumNumberOfLinks: 15}">
<f:render section="paginatedPosts" arguments="{paginatedPosts: paginatedPosts}"/>
</cr:widget.paginate>
</f:section>

<f:section name="fromNodes">
<cr:widget.paginate widgetId="posts-paginator" nodes="{nodes}" nodeTypeFilter="RobertLemke.Plugin.Blog:Post" as="paginatedPosts" configuration="{itemsPerPage: 5, insertBelow: 1, maximumNumberOfLinks: 15}">
<f:render section="paginatedPosts" arguments="{paginatedPosts: paginatedPosts}"/>
</cr:widget.paginate>
</f:section>

<f:section name="paginatedPosts">
<ol class="posts">
<f:for each="{paginatedPosts}" as="post">
<li class="post">
<h2>
<neos:link.node node="{post}">{post.properties.title}</neos:link.node>
<f:if condition="{post.removed}"> (removed)</f:if>
</h2>
<p class="content">
<neos:link.node node="{post}" class="invisible-link">
<blog:teaser node="{post}"/>
</neos:link.node> <neos:link.node node="{post}" class="read-more"><f:translate package="RobertLemke.Plugin.Blog">Read more</f:translate></neos:link.node>
</p>
</li>
<hr/>
</f:for>
</ol>
{pagination.numberOfPages}
</f:section>

0 comments on commit f78d181

Please sign in to comment.