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 sure list field |length filter works as expected #1455

Merged
merged 2 commits into from
Jun 9, 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
2 changes: 2 additions & 0 deletions src/Entity/Field/CollectionField.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Bolt\Entity\FieldParentInterface;
use Bolt\Entity\FieldParentTrait;
use Bolt\Entity\ListFieldInterface;
use Bolt\Entity\ListFieldTrait;
use Bolt\Repository\FieldRepository;
use Doctrine\ORM\Mapping as ORM;

Expand All @@ -19,6 +20,7 @@
class CollectionField extends Field implements FieldInterface, FieldParentInterface, ListFieldInterface
{
use FieldParentTrait;
use ListFieldTrait;

public const TYPE = 'collection';

Expand Down
4 changes: 2 additions & 2 deletions src/Entity/Field/FileField.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ public function getValue(): array
* Allows {% if image is empty %} in Twig
* See https://twig.symfony.com/doc/3.x/tests/empty.html
*/
public function count()
public function count(): int
{
return empty($this->getValue()['filename']);
return empty($this->getValue()['filename']) ? 0 : 1;
}
}
3 changes: 3 additions & 0 deletions src/Entity/Field/FilelistField.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@
use Bolt\Entity\Field;
use Bolt\Entity\FieldInterface;
use Bolt\Entity\ListFieldInterface;
use Bolt\Entity\ListFieldTrait;
use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity
*/
class FilelistField extends Field implements FieldInterface, ListFieldInterface
{
use ListFieldTrait;

public const TYPE = 'filelist';

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Entity/Field/ImageField.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ public function includeAlt(): bool
* Allows {% if file is empty %} in Twig
* See https://twig.symfony.com/doc/3.x/tests/empty.html
*/
public function count()
public function count(): int
{
return empty($this->getValue()['filename']);
return empty($this->getValue()['filename']) ? 0 : 1;
}
}
3 changes: 3 additions & 0 deletions src/Entity/Field/ImagelistField.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@
use Bolt\Entity\Field;
use Bolt\Entity\FieldInterface;
use Bolt\Entity\ListFieldInterface;
use Bolt\Entity\ListFieldTrait;
use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity
*/
class ImagelistField extends Field implements FieldInterface, ListFieldInterface
{
use ListFieldTrait;

public const TYPE = 'imagelist';

/**
Expand Down
5 changes: 4 additions & 1 deletion src/Entity/Field/SetField.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,19 @@
use Bolt\Entity\FieldInterface;
use Bolt\Entity\FieldParentInterface;
use Bolt\Entity\FieldParentTrait;
use Bolt\Entity\ListFieldInterface;
use Bolt\Entity\ListFieldTrait;
use Bolt\Repository\FieldRepository;
use Doctrine\ORM\Mapping as ORM;
use Tightenco\Collect\Support\Collection;

/**
* @ORM\Entity
*/
class SetField extends Field implements FieldInterface, FieldParentInterface
class SetField extends Field implements FieldInterface, FieldParentInterface, ListFieldInterface
{
use FieldParentTrait;
use ListFieldTrait;

public const TYPE = 'set';

Expand Down
2 changes: 1 addition & 1 deletion src/Entity/ListFieldInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
/**
* Any Field that has an array of fields as its value must implement this interface.
*/
interface ListFieldInterface
interface ListFieldInterface extends \Countable
{
}
17 changes: 17 additions & 0 deletions src/Entity/ListFieldTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Bolt\Entity;

trait ListFieldTrait
{
/**
* Makes ListFieldInterface fields |length filter
* return the number of elements in the field
*/
public function count(): int
{
return count($this->getValue());
}
}