Fetch fields from a selected structure

I have a structure field in site.yml, and a selection option on a page using the select-a-structure plugin.

How can I fetch the fields of the selected option in the page template?

Should be something like

$selectedOption = $page->{$field}(); // replace $field with the name of your select field
$structureField = $site->{$nameOfStructurefield}()->structure(); // replace $nameOfStructurefield with the name of the structure field you queried in the select. field
$optionKey = 'name of the field you use in the selectfield as optionkey';

$selectedField = $structureField->findBy($optionKey, $selectedOption);


I get an error: ‘Method name must be a string’


Please post complete code indicating in which line the error occurs.

Ok, this is the structure field to add ads or banners on site.yml:

    label: Ads
    type: structure
    entry: >
            label: Title
            type: text
            label: Code
            type: textarea

And this is the code to select them on article.yml :

    label: Select Ad
    type: selectastructure
    structurepage: /
    structurefield: ads
    optionkey: title

and I need to show the code field of the selected ad on the article template.

That doesn’t answer my question: Where does the error occur? Please post your template code where the error happens indicating the line where it is thrown.

Yes, in the first line:

$adSelected = $page->{$ad}();

Well, your field is called ad not $ad, so

$adSelected = $page->ad();

I used variables because you didn’t provide the information of what your fields were called. Was assuming that you know how to call a page field.

Yes, it seemed strange to me, but I thought maybe it was another way to do it. I saw that my first message wasn’t clear enough and for this reason I added the rest of the code.

Now, the message doesn’t appear but I still wonder how to fetch the code field.

    $adSelected = $page->ad()->value();
    $structureField = $site->ads()->structure();
    $optionKey = 'title';

    $selectedField = $structureField->findBy($optionKey, $adSelected);

    echo $selectedField;

You can’t echo $selectedField because it is an item from a structurefield, so as if you were looping through a structurefield, you have to call the separate fields within that field:

echo $selectedField->title();
echo $selectedField->code();

If unsure what you have, always do a dump to see what you got:


will show you that it is an object you usually can’t just echo…

You can of course use a variable as a field name, but when using variables, they have to be defined in advance. And in the case of using a variable for fieldnames, the variable must contain a string.

Ok. But something is wrong because I get this error:

‘Call to a member function code() on null.’

On line:

echo $selectedField->code();

Then $selectedField doesn’t seem to contain anything, which will be the case if the entry doesn’t exist. Are you calling this within a loop? Of course, you should always check if the object exists, so

if($selectedField) {
echo $selectedField->code();

If I do a dump, as you suggested, I can see the fields:

Structure Object
    [0] => Structure Object
            [title] => Field Object
                    [page] => 
                    [key] => title
                    [value] => Amazon Banner

            [code] => Field Object
                    [page] => 
                    [key] => code
                    [value] => Text


    [1] => Structure Object
            [title] => Field Object
                    [page] => 
                    [key] => title
                    [value] => Google Ad

            [code] => Field Object
                    [page] => 
                    [key] => code
                    [value] => Google Ad code



I don’t use a loop, I use the code as appears above.

Hm, why is your field part of an array? That seems strange… Or where does the [0] key come from?

I don’t know, it’s the first time I use this plugin.

I edited the dump and you can see the full code.

Ok, your dump returns a collection of structure items, which is weird, because findBy only returns a single entry, not the complete structure field. Sure you are dumping the right element here?

I suggest you check your code again.

The solution I offered in my first post above is correct and should work as expected.

I’m dumping the structure field, as you suggested before, to check that the fields exist.

Ok, I will try again later.

Thank you

I wanted you to dump the result of the findBy query, because that didn’t seem to yield any results, hence the call on code() didn’t work.