Closed taylorcammack closed 1 month ago
Whipped up a modifier as a work-around
<?php
namespace App\Modifiers;
use Statamic\Fields\LabeledValue;
use Statamic\Modifiers\Modifier;
class IsActuallyEmpty extends Modifier
{
protected static $handle = 'is_actually_empty';
/**
* Modify a value.
*
* @param mixed $value The value to be modified
* @param array $params Any parameters used in the modifier
* @param array $context Contextual values
* @return mixed
*/
public function index($value)
{
if ($value instanceof LabeledValue) {
if (empty(array_filter($value->toArray()))) {
return false;
}
}
return true;
}
}
Good find. Those fieldtypes give you a "labeled value" even if they're null. Probably should just give you null.
Try doing this instead: {{ if item:value }}
Yep, had tried that and it works for that scenario, but it doesn't work if you're using the same partial to pass through fields that returns simple values or null (text fields, etc.).
At this point, seems like you'd need to have a separate partial just to change that one line or go the modifier route.
Then pass :item="sprinkler_system:item"
to the partial.
Oh gotcha, assuming you mean :item="sprinkler_system:value"
- yep, that works
Oh yeah, value. 😄
Looks like I was wrong on thinking this was also a button_group
field issue, just seeing this on select
. Suppose that same pattern may be used on other fieldtypes that I just haven't used yet. Updated code example and title.
Yeah it's any that use those "labeled value" types. Button groups, selects, radios, checkboxes, etc.
Piggybacking here, as I think I'm running into the same underlying issue. Currently upgrading a site from 3.x to 4.x, and it seems like templating a multiple
-enabled select field (ie, via a loop) no longer works, similar to what Taylor outlined above—no value set in the content markdown. This used to do the trick, and antlers would just skip it if empty:
{{ organizations }}
{{ partial:components/tag-bubble }}
{{ /organizations }}
In 4.x, however, it's rendering that partial even when organizations (the multiple
-enabled select field) is empty. Which, in this case, causes the title of the article to show up as a tag, presumably from the cascade.
Workaround above does the trick (ie, wrapping in an {{ if }}
)—just wanted to share this scenario, in case it's helpful for restoring that v3 behavior somewhere down the road, so the loop works as expected by not rendering when null.
From what I can tell, both of these issues seem to be fixed on the latest version of Statamic. 🎉
Bug description
I have a
select
fieldtype that doesn't have a value set. The field doesn't appear in the entry markdown file.I'm passing that value into a partial
And inside that partial, checking for a value:
Rather than show nothing (the expected behavior) because the value isn't set, it shows the label that I passed in with no item value.
However, if I do this, it works as expected.
When I checked what is being returned, this is what I get:
By contrast, an empty text fieldtype simply returns
null
. It seems that this should be the expected behavior for empty fields.How to reproduce
select
fieldtype.Logs
No response
Environment
Installation
Other (please explain)
Antlers Parser
runtime (new)
Additional details
No response