storypioneers/kirby-selector

Get the imageurl inside an structurefield

Closed this issue · 6 comments

Hi,
If I use your selectorfield inside an structurefield. How can get the imageurl?
If I do it like in the documentation I have to break the foreach.
What can I do?

Kind regards

Could you post the structure fields related excerpt from your blueprint? And the code you have tried to use to get the image url?

Hi,
my structurefield

<?php if(!defined('KIRBY')) exit ?>

title: Page
pages: true
files: true
fields:
    title:
        label: Title
        type: text
    textbeforpartner:
        label: Text über Partnerauflistung
        type: textarea
        size: large
        icon: align-left
    partner:
        label: Kooperationspartner
        type: structure
        entry: >
            {{partnerlogo}}<br />
            {{partnername}} {{partnergenre}}
            {{partnerdescription}} {{partnerurl}}
        fields:
            partnerlogo:
                label: Partnerlogo
                type:  selector
                mode:  single
                types:
                    - image
            partnername:
                label: Partnername
                type: text
            partnergenre:
                label: Partnergeschäftsbereich
                type: text
            partnerdescription:
                label: Beschreibung
                type: textarea
                size: large
            partnerurl:
                label: Webseite
                type: url

in this template I call the imageurl width:
<?php echo $partner['partnerlogo'] ?>
but this brings only the imagename with suffix not the hole url.
Here is actually my php code:

<?php
$partners = yaml($page->partner());

$partnerimage = $page->partnerlogo()->toFile();
?>
<article class="maincontent box col-sx-24 col-sm-22 col-sm-offset-1 col-md-10 col-md-offset-6 col-lg-10 col-lg-offset-6">
    <header class="col-xs-24 col-sm-24 col-md-24 col-lg-24">
        <h1 class="col-xs-23 col-sm-23 col-md-23 col-lg-23 nogutter pull-left"><?php echo html($page->title()) ?></h1>
        <?php if($page->subtitle() !== ''): ?>
            <h2><?php echo html($page->subtitle()) ?></h2>
        <?php endif  ?>
    </header>
    <div class="headlineborder-red col-xs-5 col-sm-5 col-md-5 col-lg-5 clearfix"></div>
    <?php echo kirbytext($page->textbeforpartner()) ?>
    <div class="partnerboxes">  
        <?php foreach($partners as $partner): ?>
        <figure class="partnerbox">
            <a class="noajax" href="<?php echo $partner['partnerurl'] ?>" target="_blank">
                <img class="logo" src="<?php echo $partner['partnerlogo'] ?>">
            </a>
            <figcaption class="partnerboxcont">
                <h3><div><?php echo $partner['partnername'] ?></div><div><?php echo $partner['partnergenre'] ?></div></h3>
                <p><?php echo $partner['partnerdescription'] ?></p>
                <a class="noajax" href="<?php echo $partner['partnerlogo'] ?>" target="_blank" style="padding-bottom: 6px;"><?php echo $partner['partnerurl'] ?></a>
            </figcaption>
        </figure>
        <?php endforeach ?>
    </div>
    <?php echo html($page->text()) ?>
</article>
<?php snippet('javascript_maincontent') ?>

To get the URL I tried different ways to call.

But also the direct way like your described doesn't work also:

// Convert the filename to a full file object
$partnerlogo = $page->partnerlogo()->toFile();

// Use the file object
echo $partnerlogo->url();

Here I got the errormessage

Fatal error: Call to a member function url() on a non-object in

I would appreciate your help for getting this work. i think my problem is a problem which is getting a lot people who are not so trained in php :).

I think the way described in the docs does not work, because the field $page->partnerlogo() does not exist, your field lives within the structure field, not as a separate field in the file.

You can solve it like this:

Use

$page->image($partner['partnerlogo'])->url();

Hi texnixe,
thanks, I will try it.
But if I understand your right, the way described in the docs will never work, or?
Every selector field is not an seperate field in the structrue field. Or do I understand something wrong?

The way described in the docs works if your image field is not within a structure field.

A field within a structure field, however, cannot be accessed directly with a page method like you tried above.

Hallo texnixe,
I was a little bit confused. Now I understand also how it has to work, after you have say me the solution :).