Create pages in the user's folder

I would like the pages created by users to be saved in their folders (accounts/UUID/“posts”/“uuid-post”/post.txt)

Is there a simple way to implement this based on a page creation controller?

FYI, nothing is done with the panel.

I’m afraid this would take a lot of effort to bend the system in a way to get this done. Would it be an option to have content/posts/{uuid-user}/{uuid-post}/post.txt instead?

I developed (for fun and to see how awesome Kirby is) a social platform, with the goal of allowing users to share content, interact with posts, follow other users and manage their profiles.

Each user can create an account, customize their profile (pseudonym, bio stored in the file: user.txt), and access a dedicated profile page, currently under /content/{pseudo} created at the same time as the account.

But I have a preference for the user’s posts folder to be directly in their own folder.

Even if you are willing to dig deep into the source code to get this somehow done, I would really not do it. Sorry.

It doesn’t matter :slightly_smiling_face:, I can create files in the user’s folder but not with Kirby’s structure. I didn’t know that it wasn’t possible by default but now that I know it I can explore other things.

That being said, for this project I can’t make the UUID of the created page (the post) become the slug.

Currently when creating the page, I create an id to make it the slug:

Post form plugin
<?php

Kirby::plugin('volcanique/post-form', [
  'routes' => [
    [
      'pattern' => 'submit-post',
      'method'  => 'POST',
      'action'  => function () {
        $kirby = kirby();
        $user = $kirby->user();

        if (!$user) {
          go('login'); // Rediriger vers la page de connexion si l'utilisateur n'est pas connecté
        }

        $alerts = [];
        $success = null;
        $data = [
          'text'   => trim(get('text')),
          'images' => $_FILES['images'] ?? null,
          'origin_url' => get('origin_url') // Récupérer l'URL d'origine depuis le formulaire
        ];

        if (empty($data['text']) && empty($data['images']['name'][0])) {
          $alerts[] = 'Veuillez soumettre un texte ou une image.';
        } else {
          $kirby->impersonate('kirby');

          try {
            $pseudo = $user->content()->get('pseudo')->value();
            $userPage = page($pseudo);

            if (!$userPage) {
              throw new Exception("Page utilisateur introuvable pour le pseudo : " . $pseudo);
            }

            $slug = uniqid();
            $newPost = $userPage->createChild([
              'slug'     => $slug,
              'template' => 'post',
              'content'  => [
                'text'    => $data['text'] ?? '',
                'author'  => $user->id(),
                'pseudo'  => $pseudo,
                'date'    => date('Y-m-d H:i:s'),
                'slug'    => $slug,
              ]
            ]);

            // Gestion des images
            if (!empty($data['images']['name'][0])) {
              foreach ($data['images']['tmp_name'] as $index => $tmpName) {
                if ($tmpName) {
                  $newPost->createFile([
                    'source'   => $tmpName,
                    'template' => 'image',
                    'filename' => $data['images']['name'][$index],
                  ]);
                }
              }
            }

            // Publier immédiatement la page en mode "unlisted"
            $newPost->changeStatus('unlisted');

            // Stocker le message de succès dans une session
            $kirby->session()->set('success_message', 'Votre post a été créé avec succès.');

            // Rediriger vers l'URL d'origine après la soumission du formulaire
            go($data['origin_url']);
          } catch (Exception $e) {
            $alerts[] = 'La création de votre post a échoué : ' . $e->getMessage();
          }
        }

        return [
          'alerts'  => $alerts,
          'success' => $success,
          'data'    => $data ?? false,
        ];
      }
    ]
  ],
]);

Is there a simple way to use the uuid of the page as a slug?
I may have missed it… :upside_down_face:

You should be able to set the uuid yourself in the content array. Then Kirby will not generate an additional one for the page.