Dynamic structure blueprint

Thanks @texnixe everything done.

I came up with a rather hacky solution. I have a dynamic section and access the page via the url.

Kirby::plugin('my/plugin', [
  'blueprints' => [
    'sections/dynamic-section' => function($kirby) {
      $url = $kirby->urls()->current();
      preg_match("/\/pages\/([a-zA-Z0-9+-]+)\/?/", $url, $result);
      $pageSlug = str_replace('+', '/', $result[1]);
      $page = page($pageSlug);

      if (!page) {
        return [
          "type" => "fields",
          "fields" => [
            "error" => [
              "type" => "info",
              "theme" => "negative",
              "text" => "Couldn't find page $pageSlug.",
            ],
          ],
        ];
      }

      // read data from the page and create the dynamic structure fields ...
      $fields = ...

      return [
        'type' => 'fields',
        'fields' => [
          'form_entries' => [
            'type' => 'structure',
            'fields' => $fields
          ]
        ]
      ];
    }
  ]
]);

@texnixe do you think extracting the page slug from the url is a bad idea and is there a specific reason why the page should not be accessible in the blueprint?

1 Like

Guess it should work for this specific use case. It’s not as if you cannot use a page object, because you can if you know what page you want to get. But the blueprint doesn’t know anything about the context. I think your code would fail if you used this section on another page.

Make sure that you check if the page exists (as always).

1 Like