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

Image and File fields support twig is empty test #1419

Merged
merged 2 commits into from
May 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
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']);
}
}