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

Make ordering in {% setcontent %} more robust #1186

Merged
merged 2 commits into from Mar 15, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/Configuration/Parser/ContentTypesParser.php
Expand Up @@ -159,6 +159,15 @@ protected function parseContentType($key, array $contentType): ?ContentType

$contentType['sort'] = $this->determineSort($contentType);

// Make sure title_format is set
if (isset($contentType['title_format'])) {
$contentType['title_format'] = (array) $contentType['title_format'];
} elseif (isset($contentType['fields']['slug']['uses'])) {
$contentType['title_format'] = (array) $contentType['fields']['slug']['uses'];
} else {
$contentType['title_format'] = (array) key($contentType['fields']);
}

// Make sure taxonomy is an array.
if (isset($contentType['taxonomy'])) {
$contentType['taxonomy'] = (array) $contentType['taxonomy'];
Expand Down
21 changes: 11 additions & 10 deletions src/Storage/Directive/OrderDirective.php
Expand Up @@ -13,15 +13,6 @@
*/
class OrderDirective
{
private $coreFields = [
'id',
'createdAt',
'modifiedAt',
'publishedAt',
'depublishedAt',
'status',
];

public function __invoke(QueryInterface $query, string $order): void
{
if ($order === '') {
Expand All @@ -36,14 +27,17 @@ public function __invoke(QueryInterface $query, string $order): void
foreach ($separatedOrders as $order) {
[ $order, $direction ] = $this->createSortBy($order);

if (in_array($order, $this->coreFields, true)) {
if (in_array($order, $query->getCoreFields(), true)) {
$query->getQueryBuilder()->addOrderBy('content.' . $order, $direction);
} elseif ($order === 'author') {
$query
->getQueryBuilder()
->leftJoin('content.author', 'user')
->addOrderBy('user.username', $direction);
} else {
if (! $this->isActualField($query, $order)) {
dump("A query with ordering on a Field (`${order}`) that's not defined, will yield unexpected results. Update your `{% setcontent %}`-statement");
}
$fieldsAlias = 'fields_order_' . $query->getIndex();
$fieldAlias = 'order_' . $query->getIndex();
$translationsAlias = 'translations_order_' . $query->getIndex();
Expand Down Expand Up @@ -94,4 +88,11 @@ protected function isMultiOrderQuery(string $order): bool
{
return mb_strpos($order, ',') !== false;
}

protected function isActualField(QueryInterface $query, string $name): bool
{
$contentType = $query->getConfig()->get('contenttypes/' . $query->getContentType());

return in_array($name, $contentType->get('fields')->keys()->all(), true);
}
}
5 changes: 5 additions & 0 deletions src/Storage/QueryInterface.php
Expand Up @@ -4,6 +4,7 @@

namespace Bolt\Storage;

use Bolt\Configuration\Config;
use Doctrine\ORM\QueryBuilder;

/**
Expand Down Expand Up @@ -44,4 +45,8 @@ public function getParameter(string $key);
* Sets the value of a parameter by key name.
*/
public function setParameter(string $key, $value): void;

public function getCoreFields(): array;

public function getConfig(): Config;
}
17 changes: 16 additions & 1 deletion src/Storage/SelectQuery.php
Expand Up @@ -4,6 +4,7 @@

namespace Bolt\Storage;

use Bolt\Configuration\Config;
use Bolt\Doctrine\JsonHelper;
use Doctrine\ORM\Query\Expr\Base;
use Doctrine\ORM\Query\ParameterTypeInferer;
Expand Down Expand Up @@ -67,13 +68,17 @@ class SelectQuery implements QueryInterface
/** @var array */
private $fieldJoins = [];

/** @var Config */
private $config;

/**
* Constructor.
*/
public function __construct(?QueryBuilder $qb = null, QueryParameterParser $parser)
public function __construct(?QueryBuilder $qb = null, QueryParameterParser $parser, Config $config)
{
$this->qb = $qb;
$this->parser = $parser;
$this->config = $config;
}

/**
Expand Down Expand Up @@ -381,4 +386,14 @@ public function getAndIncrementIndex()

return $this->getIndex();
}

public function getCoreFields(): array
{
return $this->coreFields;
}

public function getConfig(): Config
{
return $this->config;
}
}
5 changes: 4 additions & 1 deletion templates/content/listing.html.twig
Expand Up @@ -41,8 +41,11 @@
:csrftoken="{{ csrf_token('batch')|json_encode }}"
></listing-select-box>

{% set titleField = contentType.title_format|first %}
{% set titleLabel = contentType.fields[titleField].label %}

{% set filterOptions = {
'id': "Id", 'title': 'Title', 'author': 'Author', 'status': 'Status', 'createdAt': 'Created date',
'id': "Id", (titleField): titleLabel, 'author': 'Author', 'status': 'Status', 'createdAt': 'Created date',
'modifiedAt': 'Modified date', 'publishedAt': 'Published date', 'depublishedAt': 'Depublished date'
} %}

Expand Down
2 changes: 1 addition & 1 deletion tests/php/Configuration/Parser/ContentTypesParserTest.php
Expand Up @@ -15,7 +15,7 @@ class ContentTypesParserTest extends ParserTestBase
{
public const NUMBER_OF_CONTENT_TYPES_IN_MINIMAL_FILE = 2;

public const AMOUNT_OF_ATTRIBUTES_IN_CONTENT_TYPE = 22;
public const AMOUNT_OF_ATTRIBUTES_IN_CONTENT_TYPE = 23;

public const AMOUNT_OF_ATTRIBUTES_IN_FIELD = 22;

Expand Down