Loop through files and a field value at the same time

Hello everyone,
My issue is the following, I’m trying to loop through all the média of my sections, the issue is some are files but some are field values vimeo links, and I want them to go along that same loop (in other words I’d like to avoid creating a foreach at the end of my document for the vimeo links)
Is that possible ?

My code looks like that :

<div class="portfolio slide" id="sections">

<?php foreach ($pages->find('projets')->children()->listed() as $project): ?>

<?php
    $section = $project->children()->listed();
    $mobileSection = $section->files()->filterBy('classDesk', '!=', 'only-desk' ); 
    $sortedMobileFiles = $mobileSection->sortBy('name', 'asc');
    $vimeoLink = $section->vimeo();

    foreach ($sortedMobileFiles as $media):?>
                <div class="visuels">

                    <?php if ($media->type() == 'image'): ?>
                        <div class="images-projets <?= $media->classDesk() ?> ">
                            <img style="object-fit: <?= $media->Fitstyle() ?>;" 
                                alt="<?= $media->alt() ?>"
                                data-lazy="<?= $media->url() ?>"
                                width="<?= $media->width() ?>"
                                height="<?= $media->height() ?>"
                            > 
                        </div>
                    <?php elseif ($media->type() == 'video'): ?>
                        <div class="images-projets">
                            <video  class="gif" loop="true" autoplay="true" muted="true" src="<?= $media->url() ?>" alt="<?= $media->alt() ?>" type="<?= $media->mime() ?>" ></video>
                        </div>
                    <?php endif; ?>
                    
                </div>

I tried adding

        <?php if ($section->vimeo()->isNotEmpty()): ?>
            <div class="images-projets <?= $section->vimeoClassDesk() ?>">
                <iframe  src="https://player.vimeo.com/video/<?= $section->vimeo()?>?dnt=1&amp;background=1&amp;" style="width:100%;height:100%;" frameborder="0" allow="autoplay; fullscreen; picture-in-picture" allowfullscreen=""></iframe>
            </div>
        <?php endif; ?>

but obviously as it is not a file it calls me on null
I look through the forum but i’m a bit confused between collections and sorted function…

Thanks in advance for your time
J

This code does not make sense. According to your definition, $section is not a single page, but a pages collection. I think giving your variables better names (plural, for example) if something contains multiple things), it helps to understand that better.

So to get all vimeo links of all the pages in the $section collection, you would have to use pluck:

$vimeoLinks = $section->pluck('vimeo', '', true);

This will return an array of links $sortedMobileFiles variable are files, and in $vimeoLinks just strings, you cannot mix them and would need a second loop to go through them.

Blockquote

This code does not make sense. According to your definition, $section is not a single page, but a pages collection. I think giving your variables better names (plural, for example) if something contains multiple things), it helps to understand that better.

I’m so sorry about that you are right, I was going all mixed up, I’m going to change that… thanks

In the end it worked with :

$vimeoLinks = $section->pluck('vimeo');
and a
<?php if ($embedMedia->isNotEmpty()): ?>
as some of them were

Thank you so much for always being so helpful :sparkles:
Have a great day