Skip to content

Commit

Permalink
Merge pull request #1428 from bolt/fix/allow-comparison-for-scalar-fi…
Browse files Browse the repository at this point in the history
…elds

Allow comparison for scalar fields
  • Loading branch information
I-Valchev authored Jun 2, 2020
2 parents 74e0024 + eceaeda commit 3fe59e3
Show file tree
Hide file tree
Showing 12 changed files with 28 additions and 45 deletions.
5 changes: 2 additions & 3 deletions config/bolt/contenttypes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ showcases:
integerfield:
type: number
mode: integer
step: 0.25
step: 1
floatfield:
type: number
mode: float
Expand Down Expand Up @@ -415,8 +415,7 @@ tests:
# Possible field types:
#
# text - varchar(256) - input type text.
# integer - integer - Input field for integer numbers.
# float - double - Input field for floating numbers, stored as 'Double'.
# number - double - Input field for numbers, with `mode: integer` or `mode: float`
# imagelist - text (65kb) - Input type for imagelists. Add multiple images. Useful for image sliders, galleries, etcetera.
# image - varchar(256) - image select/upload widget, stored as filename.
# file - varchar(256) - file select/upload widget, stored as filename.
Expand Down
3 changes: 2 additions & 1 deletion src/Entity/Content.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
use Bolt\Configuration\Content\ContentType;
use Bolt\Entity\Field\Excerptable;
use Bolt\Entity\Field\ScalarCastable;
use Bolt\Enum\Statuses;
use Bolt\Repository\FieldRepository;
use Doctrine\Common\Collections\ArrayCollection;
Expand Down Expand Up @@ -640,7 +641,7 @@ public function __call(string $name, array $arguments = [])
throw new \RuntimeException(sprintf('Invalid field name or method call on %s: %s', $this->__toString(), $name));
}

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

Expand Down
7 changes: 6 additions & 1 deletion src/Entity/Field/CheckboxField.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@
/**
* @ORM\Entity
*/
class CheckboxField extends Field implements FieldInterface
class CheckboxField extends Field implements FieldInterface, ScalarCastable
{
public const TYPE = 'checkbox';

public function getTwigValue()
{
return current($this->getValue());
}
}
2 changes: 1 addition & 1 deletion src/Entity/Field/DateField.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
/**
* @ORM\Entity
*/
class DateField extends Field implements FieldInterface
class DateField extends Field implements FieldInterface, ScalarCastable
{
public const TYPE = 'date';

Expand Down
2 changes: 1 addition & 1 deletion src/Entity/Field/EmailField.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
/**
* @ORM\Entity
*/
class EmailField extends Field implements FieldInterface
class EmailField extends Field implements FieldInterface, ScalarCastable
{
public const TYPE = 'email';
}
17 changes: 0 additions & 17 deletions src/Entity/Field/FloatField.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/Entity/Field/HiddenField.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
/**
* @ORM\Entity
*/
class HiddenField extends Field implements FieldInterface
class HiddenField extends Field implements FieldInterface, ScalarCastable
{
public const TYPE = 'hidden';

Expand Down
17 changes: 0 additions & 17 deletions src/Entity/Field/IntegerField.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/Entity/Field/NumberField.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
/**
* @ORM\Entity
*/
class NumberField extends Field implements FieldInterface
class NumberField extends Field implements FieldInterface, ScalarCastable
{
public const TYPE = 'number';
}
12 changes: 12 additions & 0 deletions src/Entity/Field/ScalarCastable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Bolt\Entity\Field;

/**
* Field that can be cast to a plain scalar value (string, int, boolean, float) in excerpt must implement this interface
*/
interface ScalarCastable
{
}
2 changes: 1 addition & 1 deletion src/Entity/Field/SlugField.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
/**
* @ORM\Entity
*/
class SlugField extends Field implements FieldInterface
class SlugField extends Field implements FieldInterface, ScalarCastable
{
public const TYPE = 'slug';

Expand Down
2 changes: 1 addition & 1 deletion templates/helpers/_field_blocks.twig
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@

{# Checkbox fields #}
{% if type == "checkbox" %}
<p><strong>{{ field|label }}</strong>: {{ field ? "checked" : "not checked" }}</p>
<p><strong>{{ field|label }}</strong>: {% if field.twigValue %} checked {% else %} not checked {% endif %}</p>
{% endif %}

{# Embed fields #}
Expand Down

0 comments on commit 3fe59e3

Please sign in to comment.