I have the following code where I want to check if my collection contains the current block…
<?php if($Upcoming->has($block)): ?>
but it’s not working yet. Is it even possible?
I have the following code where I want to check if my collection contains the current block…
<?php if($Upcoming->has($block)): ?>
but it’s not working yet. Is it even possible?
What is $Upcoming
? And what $block
?
My Collection “Upcoming”:
$newBlocks = $site->newBlocks()->toBlocks();
$Upcoming = $newBlocks
->filter(function ($block) {
return $block->from()->toDate() > time();
})
->sortBy(function ($site) {
return $site->from()->toDate();
}, 'desc');
and the $block is a snippet in snippets/blocks/block.php
I’m getting this: “Call to a member function has() on null” in block type: “block”
I already tried to create this collection right in front of my if-statement
Hm, the error means that $Upcoming
is null instead of a blocks collections. You can check this with dump($Upcoming);
then, I am getting this:
Kirby\Cms\Blocks Object
(
[0] => 451e65c0-85e1-48d5-b364-edc1bea6bb0d
)
but on my page where I created the collection, I am displaying blocks out of this collection - so it works…
with this code:
<?php
if ($Upcoming->count() > 0): ?>
<ul class="munich-content-list">
<?php foreach ($Upcoming as $block): ?>
<li class="event-block">
<div id="<?= $block->id() ?>" class="block block-type-<?= $block->type() ?>">
<?= $block ?>
</div>
<?php endforeach ?>
</li>
</ul>
<?php endif ?>
So the collection does in fact exist, don’t know why you are getting this error, then. Maybe you can post the complete block snippet.
Hmm, strange. It’s not that important – It works when I put “$block->from()->toDate() > time()” in my if-statement too.
I just wanted it to be more dynamic/shorter.
Thanks anyway!