filterBy('intendedTemplate', '....' ) does not return the intended page object

Dear all,

1/ Background: One-pager project => The backend (panel) allows the editor to specify page data (‘elements’ = ‘ele’) for rendering. The home.php template loops through all elements and calls the related ‘intendedTemplate’ snippets, which works well:

<?php foreach ($pages->listed() as $section): ?>
 <?php if ($section and $section->isNotEmpty()): ?>
  <?php snippet('ele.' . $section->intendedTemplate() . '.snippet', ['data' => $section]) ?>
 <?php endif ?>
<?php endforeach ?>

2/ Special case: at additional places in the html, code needs to be inserted, beside the looped elements.

3/ filterBy: For the insertion of the dedicated code, I use the ‘filterBy’ method to specify the intended snippet for code insertion, but the method does not work as expected by me.

4/ my template: home.php
[…]
<?php snippet('ele.socialmediapanel.snippet', ['data' => $pages->listed()->filterBy('intendedTemplate', 'socialmediapanel')]) ?>
[…]

my snippet: socialmediapanel.php
Surrounding html selectors are suppressed by the comment editor

<?php var_dump($data) ?>

/site/blueprints/pages contains a ‘socialmediapanel.yml’ with section definitions.
/content/4_sm contains a 'socialmediapanel.txt’ with the page data.

5/ Problem: the snippet call works, but the filterBy() method seems not to return a page object to the $data variable (respectively I cannot access the page data) while the foreach loop does return a page object (respectively I can access the page data).
In the snippet, the $data variables are dumped. The filterBy() induced dump does not contain page data, while the loop induced dump does.
I checked the Filtering compendium and understand that filterBy() might be tricky (e.g. example “Filter by pages field”). I can share var_dump() results, if requested.

What is my mistake respectively how can I filter a dedicated page using filterBy() and get the intended page data?

Thank you for your help!

Just shooting from the hip: I assume the filterBy method to return a collection, not a single page object. Then $data will contain an array, not a single value.

Just a guess without more elaboration.

1 Like

This if statement doesn’t make sense- $section in this case is definitely a page object, so you don’t have to check if it exists, and isNotEmpty() is a collection and a field method, but not a page method. So delete this if statement.

I don’t know what you have in your socialmediapanel snippet, but in all the other snippets, you pass a page object to the $data variable, here you suddenly pass a filtered collection. What is the purpose?

A page object cannot be filtered, only a collection. Not clear what you want to achieve.

1 Like

Thank you for your advices.

What do I want to achieve?
=> A static test one-pager website shall be transformed to Kirby CMS. (So far, the transformation on the local dev machine is progressing well.)

The test website renders three Social Media panels (top, slider menu, footer). In the CMS backend, the editor shall only copy once the Social Media & Maps information (=> i.e. apply just one socialmediapanel.yml) and indicate via toggle switches, where the social media panels shall be rendered (top, menu and/or footer). For the rendering of the up to three social media panels, only one template snippet should be used (with a variable handed-over to control, which of the three panel version is to be rendered). Each panel needs to rendered at dedicated places in the html file.

I intend to command Kirby to use the socialmediapanel.php template snippet and hand-over the appropriate page data as variable “$data”. But the “$data” hand over using findBy(‘intendTemplate’, …) does not work as expected.
=> How can I filter the page with the Social Media & Maps information (="$data") and command the rendering at specific places in the html code depending on control information provided by “$data”.

Is the issue better understandable now? What would you advice me to solve the problem?
Thanks a lot for your support.