Page lookup on a pages field that is in a block

I’ve found a way that works as a proof of concept but maybe there is a much better way to do this.

I have a liste of pages stored in my content/ressources folder. The aim is to display in those ressource pages if they are referenced elsewhere. The reference is made possible by my default.yml blueprint through a layout field with a custom block named documentlink in its fieldsets. So i need to search through all the site pages. See below the concerned files:

File site/controllers/ressource.php

<?php

return function ($kirby, $page) {

    $relatedPages = $kirby->site()->pages()->filter(function ($relatedPage) use ($page) {
        foreach ($relatedPage->layout()->toLayouts() as $layout) {
            foreach ($layout->columns() as $column) {
                foreach ($column->blocks() as $block) {
                    if ($block->type() === 'documentlink') {
                        $linkedPage = $block->ressource()->toPage();
                        if ($linkedPage && $linkedPage->id() === $page->id()) {
                            return true;
                        }
                    }
                }
            }
        }
        return false;
    });

    return [
        'relatedPages' => $relatedPages
    ];
};

File site/blueprints/pages/default.yml

title: Page de base

columns:
  main:
    width: 2/3
    sections:
      fields:
        type: fields
        fields:
          layout:
            type: layout
            label: Mise en page
            layouts:
              - "1/1"
              - "1/2, 1/2"
              - "1/3, 1/3, 1/3"
              - "1/4, 1/4, 1/4, 1/4"
              - "1/4, 1/2, 1/4"
              - "1/3, 2/3"
              - "2/3, 1/3"
            fieldsets:
              text:
                label: Texte
                type: group
                fieldsets:
                  - heading
                  - text
                  - list
                  - quote
                  - line
                  - documentlink
              media:
                label: Média
                type: group
                fieldsets:
                  - image
                  - gallery
                  - video

File site/plugins/block-factory/blueprints/blocks/documentlink.yml

type: documentlink
icon: document
fields:
  ressource:
    type: pages
    multiple: false
    label: Ressource sélectionnée
    query: kirby.page('ressources').children.listed

This is the early phase of the project so i’m eager to find any better way to solve this if there are any. The website will not grow too big so i’m not concerned about the performance aspect of the code.

Thanks in advance and happy coding!
Sean.

The code above can be shortened by directly converting to blocks:

$relatedPages = $kirby->site()->pages()->filter(function ($relatedPage) use ($page) {
	foreach ($relatedPage->layout()->toBlocks()->filterBy('type', 'documentlink')  as $block) {
        if (($linkedPage = $block->ressource()->toPage()) && $linkedPage->id() === $page->id()) {
            return true;
        }
	}
	return false;
});
1 Like

That’s a very nice readability improvement, thanks!