Skip to content

Commit

Permalink
Merge pull request #1419 from bolt/feature/field-is-empty
Browse files Browse the repository at this point in the history
Image and File fields support twig empty test
  • Loading branch information
bobdenotter authored May 29, 2020
2 parents ac02b67 + da75e92 commit 6ff15a7
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
7 changes: 6 additions & 1 deletion src/Entity/Content.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use ApiPlatform\Core\Annotation\ApiSubresource;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
use Bolt\Configuration\Content\ContentType;
use Bolt\Entity\Field\Excerptable;
use Bolt\Enum\Statuses;
use Bolt\Repository\FieldRepository;
use Doctrine\Common\Collections\ArrayCollection;
Expand Down Expand Up @@ -639,7 +640,11 @@ public function __call(string $name, array $arguments = [])
throw new \RuntimeException(sprintf('Invalid field name or method call on %s: %s', $this->__toString(), $name));
}

return $field->getTwigValue();
if ($field instanceof Excerptable) {
return $field->getTwigValue();
}

return $field;
}

/**
Expand Down
12 changes: 11 additions & 1 deletion src/Entity/Field/FileField.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@

use Bolt\Entity\Field;
use Bolt\Entity\FieldInterface;
use Countable;
use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity
*/
class FileField extends Field implements FieldInterface
class FileField extends Field implements FieldInterface, Countable
{
use FileExtrasTrait;

Expand Down Expand Up @@ -52,4 +53,13 @@ public function getValue(): array

return $value;
}

/**
* Allows {% if image is empty %} in Twig
* See https://twig.symfony.com/doc/3.x/tests/empty.html
*/
public function count()
{
return empty($this->getValue()['filename']);
}
}
12 changes: 11 additions & 1 deletion src/Entity/Field/ImageField.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@
use Bolt\Entity\Media;
use Bolt\Repository\MediaRepository;
use Bolt\Utils\ThumbnailHelper;
use Countable;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Asset\PathPackage;
use Symfony\Component\Asset\VersionStrategy\EmptyVersionStrategy;

/**
* @ORM\Entity
*/
class ImageField extends Field implements FieldInterface, MediaAwareInterface
class ImageField extends Field implements FieldInterface, MediaAwareInterface, Countable
{
use FileExtrasTrait;

Expand Down Expand Up @@ -99,4 +100,13 @@ public function includeAlt(): bool

return $this->getDefinition()->get('alt') === true;
}

/**
* Allows {% if file is empty %} in Twig
* See https://twig.symfony.com/doc/3.x/tests/empty.html
*/
public function count()
{
return empty($this->getValue()['filename']);
}
}

0 comments on commit 6ff15a7

Please sign in to comment.