Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Singletons return single Content result with {% setcontent %} #1565

Merged
merged 7 commits into from
Jun 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Controller/TwigAwareController.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ protected function createPager(Query $query, string $contentType, int $pageSize,
{
$params = [
'status' => '!unknown',
'returnmultiple' => true,
];

if ($this->request->get('sortBy')) {
Expand Down
3 changes: 3 additions & 0 deletions src/Storage/ContentQueryParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Bolt\Storage\Directive\OffsetDirective;
use Bolt\Storage\Directive\OrderDirective;
use Bolt\Storage\Directive\PrintQueryDirective;
use Bolt\Storage\Directive\ReturnMultipleDirective;
use Bolt\Storage\Directive\ReturnSingleDirective;
use Bolt\Storage\Handler\FirstQueryHandler;
use Bolt\Storage\Handler\IdentifiedSelectHandler;
Expand Down Expand Up @@ -80,6 +81,7 @@ class ContentQueryParser

/** @var Environment */
private $twig;

/** @var Notifications */
private $notifications;

Expand Down Expand Up @@ -119,6 +121,7 @@ protected function setupDefaults(): void
$this->addDirectiveHandler('page', new OffsetDirective());
$this->addDirectiveHandler('printquery', new PrintQueryDirective());
$this->addDirectiveHandler('returnsingle', new ReturnSingleDirective());
$this->addDirectiveHandler('returnmultiple', new ReturnMultipleDirective());
}

/**
Expand Down
18 changes: 18 additions & 0 deletions src/Storage/Directive/ReturnMultipleDirective.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Bolt\Storage\Directive;

use Bolt\Storage\SelectQuery;

/**
* Directive to specify that an array, rather than a single object should be returned.
*/
class ReturnMultipleDirective
{
public function __invoke(SelectQuery $query): void
{
$query->setSingleFetchMode(false);
}
}
3 changes: 0 additions & 3 deletions src/Storage/Handler/IdentifiedSelectHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ public function __invoke(ContentQueryParser $contentQuery)
} else {
$contentQuery->setParameter('slug', $contentQuery->getIdentifier());
}
if (count($contentQuery->getContentTypes()) === 1) {
$contentQuery->setDirective('returnsingle', true);
}

return call_user_func($contentQuery->getHandler('select'), $contentQuery);
}
Expand Down
4 changes: 4 additions & 0 deletions src/Storage/Handler/LatestQueryHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ public function __invoke(ContentQueryParser $contentQuery): Pagerfanta
{
$contentQuery->setDirective('order', '-id');

// If we're using `/latest`, always return a paginator, even for Singletons
$contentQuery->setDirective('returnsingle', false);
$contentQuery->setDirective('returnmultiple', true);

return $contentQuery->getHandler('select')($contentQuery);
}
}
3 changes: 1 addition & 2 deletions src/Storage/Handler/SelectQueryHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ public function __invoke(ContentQueryParser $contentQuery)

/** @var SelectQuery $selectQuery */
$selectQuery = $contentQuery->getService('select');
$selectQuery->setSingleFetchMode(false);

$selectQuery->setQueryBuilder($qb);
$selectQuery->setContentTypeFilter($contentQuery->getContentTypes());
Expand All @@ -46,7 +45,7 @@ public function __invoke(ContentQueryParser $contentQuery)

$contentQuery->runDirectives($selectQuery);

if ($selectQuery->getSingleFetchMode()) {
if ($selectQuery->shouldReturnSingle()) {
return $qb
->setMaxResults(1)
->getQuery()
Expand Down
33 changes: 31 additions & 2 deletions src/Storage/SelectQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Bolt\Storage;

use Bolt\Configuration\Config;
use Bolt\Configuration\Content\ContentType;
use Bolt\Doctrine\JsonHelper;
use Doctrine\ORM\Query\Expr\Base;
use Doctrine\ORM\Query\ParameterTypeInferer;
Expand Down Expand Up @@ -42,7 +43,7 @@ class SelectQuery implements QueryInterface
protected $replacements = [];

/** @var bool */
protected $singleFetchMode = false;
protected $singleFetchMode = null;

/** @var int */
protected $index = 1;
Expand Down Expand Up @@ -113,6 +114,34 @@ public function getContentType(): string
return $this->contentType;
}

public function isSingleton(): bool
{
/** @var ContentType|null $definition */
$definition = $this->config->get('contenttypes/' . $this->contentType);

// We only allow this, if getSingleFetchMode wasn't explicitly set
if ($this->getSingleFetchMode() === false) {
return false;
}

return $definition ? $definition->get('singleton') : false;
}

public function shouldReturnSingle(): bool
{
// We only allow this, if getSingleFetchMode wasn't explicitly set
if ($this->getSingleFetchMode() === false) {
return false;
}

// If we're in an "IdentifiedSelect", always return a single
if (array_key_exists('slug_1', $this->getWhereParameters())) {
return true;
}

return $this->getSingleFetchMode() || $this->isSingleton();
}

/**
* Sets the parameters that will filter / alter the query.
*/
Expand Down Expand Up @@ -261,7 +290,7 @@ public function setQueryBuilder($qb): void
/**
* Returns whether the query is in single fetch mode.
*/
public function getSingleFetchMode(): bool
public function getSingleFetchMode(): ?bool
{
return $this->singleFetchMode;
}
Expand Down