How can i check if all the structured fields are not empty, and only display if everything is filled in?
Cheers, Maarten
<section class="column">
<div class="pricing-cards-eq mb-3">
<?php $products = $module->products()->yaml() ?>
<?php foreach($products as $product): ?>
<?php
$string = $product['features'];
$features = str::split($string, $separator = '|');
?>
<div class="pricing-card pricing-card-vertical pricing-card-eq">
<h2 class="pricing-card-name alt-h3"><?php echo $product['title'] ?></h2>
<div class="pricing-card-text">
<p>
<span class="alt-h2 mr-2"><span class="default-currency"><?php echo $product['price'] ?></span></span>
</p>
<p class="alt-text-small text-gray"><?php echo $product['description'] ?></p>
<ul class="features">
<?php foreach($features as $feature):?>
<li><?php echo kirbytext($feature) ?></li>
<?php endforeach ?>
</ul>
</div>
<div class="pricing-card-cta">
<a href="<?= $pages->find($product['ctalink'])->url() ?>" class="button"><?php echo $product['cta'] ?></a>
</div>
</div>
<?php endforeach ?>
</div>
</section>
<?php
// for my own taste i like using toStructure() instead of yaml() most of the time
foreach($page->somestructure()->toStructure() as $product){
if($product->field1()->value() && $product->field2()->value()){
// DO if all set.
}
}
?>
1 Like
With yaml:
<?php
$products = $module->products()->yaml();
foreach($products as $product) {
if(count(array_filter($product, function($item) {
return empty($item);
})) == 0) {
// do something
}
}
1 Like
Thank you both, love the solutions!!
krisa
May 13, 2017, 2:54pm
5
@texnixe maybe it would be nice improvenment if fileld method
$methods['empty']
took care of it somehow (atleast with error message)?
Its quite confusing because you get something out and its wrong
@krisa Didn’t you say you didn’t want to post anything anywhere but in the chat anymore? So here you are!
I’m not sure I understand your question? If you use toStructure()
on the structure field, you can, of course, check if a field is empty or not:
<?php
$structure = $site->structurefield()->toStructure();
foreach($structure as $s) {
if($s->field1()->isNotEmpty() && $s->field2()->isNotEmpty() && $s->field3()->isNotEmpty()) {
echo $s->field1() . $s->field2() . $s->field3();
}
}
Or using filter($callback)
:
<?php
$structure = $site->structurefield()->toStructure();
$filteredStructure = $structure->filter(function($child){
return $child->field1()->isNotEmpty() && $child->field2()->isNotEmpty() && $child->field3()->isNotEmpty();
});
krisa:
$structure->isNotEmpty()
What is this supposed to do? $structure
in the above example is not a field, but a collection; so you can’t use a field method with it.
I was just trying to do this myself. This seems to work pretty well, if you need to know before the loop has started:
<?php
$structure = $site->structurefield()->toStructure();
if($structure->first()) {
// do something
} else {
// something else
}
1 Like
I’d use $structure->count()
to stay in line with Kirby’s internal checks… but yes, first()
should work as well.
1 Like