Skip to content

Commit

Permalink
add ncr_is_node_published and $andClear option to getting preloaded n…
Browse files Browse the repository at this point in the history
…odes
  • Loading branch information
gdbrown committed Feb 7, 2019
1 parent bcbb7d0 commit b21987b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG-0.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
This changelog references the relevant changes done in 0.x versions.


## v0.3.10
* Add `$andClear` argument to `NcrExtension::getPreloaded[Published]Nodes(bool $andClear = true)` so twig functions `ncr_get_preloaded_nodes` and `ncr_get_preloaded_published_nodes` don't return the same preloaded nodes.
* Add `NcrExtension::isNodePublished` and twig function `ncr_is_node_published`.


## v0.3.9
* Update `gdbots_ncr.ncr_request_interceptor` service definition to provide `cache.app` as first argument since `gdbots/ncr` v0.3.11 adds caching for slug lookups.

Expand Down
38 changes: 34 additions & 4 deletions src/Twig/NcrExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Gdbots\Pbj\Message;
use Gdbots\Pbj\MessageRef;
use Gdbots\Pbj\WellKnown\Identifier;
use Gdbots\Schemas\Ncr\Enum\NodeStatus;
use Gdbots\Schemas\Ncr\Mixin\Node\Node;
use Gdbots\Schemas\Ncr\NodeRef;
use Psr\Log\LoggerInterface;
Expand Down Expand Up @@ -56,6 +57,7 @@ public function getFunctions()
new \Twig_SimpleFunction('ncr_get_node', [$this, 'getNode']),
new \Twig_SimpleFunction('ncr_get_preloaded_nodes', [$this, 'getPreloadedNodes']),
new \Twig_SimpleFunction('ncr_get_preloaded_published_nodes', [$this, 'getPreloadedPublishedNodes']),
new \Twig_SimpleFunction('ncr_is_node_published', [$this, 'isNodePublished']),
new \Twig_SimpleFunction('ncr_preload_node', [$this, 'preloadNode']),
new \Twig_SimpleFunction('ncr_preload_nodes', [$this, 'preloadNodes']),
new \Twig_SimpleFunction('ncr_preload_embedded_nodes', [$this, 'preloadEmbeddedNodes']),
Expand Down Expand Up @@ -111,19 +113,47 @@ public function getNode($ref): ?Node
}

/**
* @param Node $node
*
* @return bool
*/
public function isNodePublished($node): bool
{
if (!$node instanceof Node) {
return false;
}

return NodeStatus::PUBLISHED()->equals($node->get('status'));
}

/**
* @param bool $andClear
*
* @return Node[]
*/
public function getPreloadedNodes(): array
public function getPreloadedNodes(bool $andClear = true): array
{
return $this->ncrPreloader->getNodes();
$nodes = $this->ncrPreloader->getNodes();
if ($andClear) {
$this->ncrPreloader->clear();
}

return $nodes;
}

/**
* @param bool $andClear
*
* @return Node[]
*/
public function getPreloadedPublishedNodes(): array
public function getPreloadedPublishedNodes(bool $andClear = true): array
{
return $this->ncrPreloader->getPublishedNodes();
$nodes = $this->ncrPreloader->getPublishedNodes();
if ($andClear) {
$this->ncrPreloader->clear();
}

return $nodes;
}

/**
Expand Down

0 comments on commit b21987b

Please sign in to comment.