$site variable not working in Builder plugin snippet

I get an error when i try to use $site inside a builder snippet. Basically i’ve set up a builder field that allows you to pick any page from the site from a select and show a small summary of the page with a button that links to it. Its like a teaser panel.

Heres my blueprint:

    title: Home
    pages: true
    files: true
    fields:
      metadata: metadata
      pagecontent:
        label: Page Content
        type: headline
      text:
        label: Content
        type: textarea
        help: The pages content
      builder:
        label: Home Page Modules
        type: builder
        fieldsets:
          parralax:
            label: Parallax Panel
            snippet: builder/parralax
            fields:
              podpicture:
                label: Image
                type: image
                width: 1/3
              podtextsource:
                label: Text Source
                type: select
                options: pages
                width: 1/3
              podtextposition:
                label: Text Position
                type: select
                default: center
                options:
                  center: Center
                  left: Left
                  right: Right
                width: 1/3

Heres my Builder snippet:

    <?php
    $articleurl = $site->index()->filterBy('uid', $data->podtextsource());
    ?>

    <a href="<?= $articleurl ?>" class="button">Read more</a>

Which works on the actual site, but in the builder area in the panel i get the following error message:

Undefined variable: site

How can this be? I thought $site was like the root of the site whereas $page works from the current page as the starting point. If i can’t use $site how can i find any page in the site from any page?

Thanks.

Maybe you can access the site via the $page variable? $page->site()? Or kirby()->site()?

1 Like

Ah ha! $page->site() worked. Thanks.