Access to toggle inside a structure

Greetings everyone,

I’m having some trouble accessing a toggle field inside a structure field and I tried many different ways, but i don’t know what it’s wrong, here is my code:

         <?php if($infoList->isNotEmpty()):?>
                <?php foreach ($infoList as $infoItem) : ?>
                    <div class="col grid col-1-1 items-center">
                        <i class="aw-<?= $infoItem->infoClass() ?> fs-48 mb-10 accent-light"></i>
                        <p class="mb-10 bold"><?= $infoItem->infoName() ?></p>
                        <p><?= $infoItem->infoDesc() ?></p>
                        <?php if ($infoItem->infoToggleCustom()->toBool() === true): ?>            
                            <p><a href="
                            <?php if ($infoItem->infoToggle()->toBool() === false): ?>
                                <?= $infoItem->infoPageLink()->url() ?>
                            <?php else: ?>
                                <?= $infoItem->infoFileLink()->url() ?>
                            <?php endif ?>"
                            class="bold"><?= $infoItem->infoTextLink() ?></a></p>
                        <?php endif ?>
                    </div>
                <?php endforeach ?>
            <?php endif; ?>

Any thoughts on this?

Thank you

What exactly is the problem?

And I assume infoPageLink and infoFileLink are values that come from a pages and files field, respectively? If so, you would have to turn convert these values into a page/file object first, before you can call the url() method.

Example:

<?php if ( $f =  $infoItem->infoFileLink()->toFile() ) : ?>
  <?= $f->url() ?>
<?php endif ?>

From a code style perspective, I’d move these if statements out of the href definition, the way you currently have it is rather unreadable code.

<?php if ($infoList->isNotEmpty()) : ?>
  <?php foreach ($infoList as $infoItem) : ?>
    <div class="col grid col-1-1 items-center">
      <i class="aw-<?= $infoItem->infoClass() ?> fs-48 mb-10 accent-light"></i>
      <p class="mb-10 bold"><?= $infoItem->infoName() ?></p>
      <p><?= $infoItem->infoDesc() ?></p>
      <?php if ($infoItem->infoToggleCustom()->toBool()) : ?>
        <?php
        $url = '';
        if ($infoItem->infoToggle()->toBool() === false && $p = $infoItem->infoPageLink()) : ?>
          <?php $url = $p->url() ?>
        <?php elseif ($infoItem->infoToggle()->toBool() === true && $f = $infoItem->infoFileLink()) : ?>
          <?php $url = $f->url(); ?>
          <?php endif ?>
          <p><a href="<?= $url ?>" class="bold"><?= $infoItem->infoTextLink() ?></a></p>
        <?php endif ?>
    </div>
  <?php endforeach ?>
<?php endif; ?>

Ideally, the logic to fetch the correct url would go into a function to keep the template clean.

Hi @texnixe, thank you for the feedback on it and you are right in terms getting everything nicer and tidy with the code.

One of the main issues has been with the toggle option, by some reason it does not work when I tried to fetch it.

At this moment with your code or mine the error is the same:

Whoops\Exception\ErrorException thrown with message "Array to string conversion"

Stacktrace:
#18 Whoops\Exception\ErrorException in D:\Work\websites\authentikwest\kirby\src\Cms\Field.php:226
#17 Whoops\Run:handleError in D:\Work\websites\authentikwest\kirby\src\Cms\Field.php:226
#16 Kirby\Cms\Field:toString in D:\Work\websites\authentikwest\kirby\src\Cms\Field.php:126
#15 Kirby\Cms\Field:__toString in D:\Work\websites\authentikwest\site\snippets\info.php:27
#14 include in D:\Work\websites\authentikwest\kirby\src\Toolkit\F.php:403
#13 Kirby\Toolkit\F:loadIsolated in D:\Work\websites\authentikwest\kirby\src\Toolkit\F.php:380
#12 Kirby\Toolkit\F:load in D:\Work\websites\authentikwest\kirby\src\Toolkit\Tpl.php:35
#11 Kirby\Toolkit\Tpl:load in D:\Work\websites\authentikwest\kirby\config\components.php:288
#10 Kirby\Cms\App:{closure} in D:\Work\websites\authentikwest\kirby\src\Cms\App.php:1391
#9 Kirby\Cms\App:snippet in D:\Work\websites\authentikwest\kirby\config\helpers.php:677
#8 snippet in D:\Work\websites\authentikwest\site\templates\home.php:107
#7 include in D:\Work\websites\authentikwest\kirby\src\Toolkit\F.php:403
#6 Kirby\Toolkit\F:loadIsolated in D:\Work\websites\authentikwest\kirby\src\Toolkit\F.php:380
#5 Kirby\Toolkit\F:load in D:\Work\websites\authentikwest\kirby\src\Toolkit\Tpl.php:35
#4 Kirby\Toolkit\Tpl:load in D:\Work\websites\authentikwest\kirby\src\Cms\Template.php:167
#3 Kirby\Cms\Template:render in D:\Work\websites\authentikwest\kirby\src\Cms\Page.php:1172
#2 Kirby\Cms\Page:render in D:\Work\websites\authentikwest\kirby\src\Cms\App.php:686
#1 Kirby\Cms\App:io in D:\Work\websites\authentikwest\kirby\src\Cms\App.php:1039
#0 Kirby\Cms\App:render in D:\Work\websites\authentikwest\index.php:5

Kind regards

It seems there was an issue with infoPageLink() and infoFileLink().

Forgot about adding infoPageLink()->toPage->url() and infoFileLink()->toFile()->url()

It works perfectly now, thank you so much for your time and have a nice weekend.