Creating a directory within a page folder after page creation

Hi!

I’m working on an image gallery. Each project in the gallery should contain a cover image that will be visible on the website, as well as a downloadable .png and .svg file. In order to keep my folders somewhat structured I want to have a separate folder within the project folder for the downloadable files. I tried using the ‘hooks’ method in my config.php to automatically create the holder when a new project page is created:


<?php

return [
    'debug'  => true,
    'languages' => true,
    'date.handler' => 'intl',
    'home' => 'portal',
    'hooks' => [
        'page.create:after' => function ($page) {
            if ($page->intendedTemplate() === 'data') {
                $downloadsFolder = $page->root() . '/downloads';
                if (!is_dir($downloadsFolder)) {
                    mkdir($downloadsFolder, 0755, true);
                }
            }
        },
    ],
];

In my blueprint for the project page I created a separate field for uploading the downloadable files, as such:


- width: 1/2
    sections:
      gallery:
        headline: Gallery
        type: files
        min: 1
        layout: cards
        template: image
      downloads:
        headline: Downloads
        type: files
        min: 2
        layout: cards
        template: downloads
        root: projects/{{ page.slug }}/downloads

For some reason the directory is not being created. I also double checked that the intendedTemplate() is correct. I can’t seem to figure out where the issue lies. Any ideas?

Must be `$page->intendedTemplate()->name() === ‘data’

$page->intendedTemplate()

returns a template object, not a string, therefore the comparison fails.

1 Like

Thank you, that worked! However, when i upload files in the panel, they don’t end up in the ‘downloads’ folder. Is this path false?

root: projects/{{ page.slug }}/downloads

or should the destination be defined in the file template somehow?

A files section doesn’t have a root property. You need to set the parent.

parent: page.children.find("data")

That did the trick, thank you so much!