Skip to content

Commit

Permalink
Merge pull request #1455 from bolt/bugfix/list-fields-lenght
Browse files Browse the repository at this point in the history
Make sure list field |length filter works as expected
  • Loading branch information
bobdenotter authored Jun 9, 2020
2 parents 1a6e91d + 7e2fa12 commit 790e2e7
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 6 deletions.
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());
}
}

0 comments on commit 790e2e7

Please sign in to comment.