Check for empty fields in a structure field outside of the loop

Hey

I am looking for a function or an idea to check if a specific field of a structure field is empty or isNotEmpty – But outside the loop.

<?php if ($page->socialmedia()->toStructure()->each('smLink')->isNotEmpty()): ?>
<h5 class="padding_bottom_mini padding_top_medium">Social Media</h5>
<?php endif; ?>
            
<div>
  <?php foreach ($page->socialmedia()->toStructure() as $channel): ?>

  <?php if ($channel->smLink()->isNotEmpty() ): ?> 
  <a class="sm_icon no_underline footer" href="<?= $channel->smLink() ?>">
       <?= asset('assets/img/facebook.svg')->read() ?></a>
   <?php endif; ?>
 ...
 <?php endforeach; ?>

First Line: Obviously, I made up the ->each() function, to give you an idea what I am looking for. :man_detective:

The problem is that the structure field has always fields, because they get created by a form on the front-end with a page update function. But they are not necessarily filled with information.
In that case, I want to hide the Headline as well.

Thanks for reading

$smLinks = $page->socialmedia()->toStructure()->pluck('smLink', ',');
if (count($smLinks)) {
  // do stuff
}

Or

$smLinks = $page->socialmedia()->toStructure()->filter(fn ($item) => $item->smLink()->isNotEmpty());
if ($smLinks->isNotEmpty()) {
 // do stuff
}
1 Like

Thanks.
good idea!