Hi Guys,
I’m trying to build a webpage everytime a user is created. Currently I’m using the hook as follows:
kirby()->hook('panel.user.create', function($user){
try {
$newAbsolvent = page('home/absolventen-arbeiten')->children()->create($user->firstName().'-'.$user->lastName(), 'absolvent', array(
'name' => $user->firstName(),
'surname' => $user->lastName(),
'email' => $user->email()
));
} catch(Exception $e) {
echo $e->getMessage();
}
});
This does create a page. However, the template for this page has some 'build:'s in it, that are not triggered, when creating the page this way. Is there a possibility to trigger them manually?
Kind regards,
Slawa
This sounds quite interesting, but I’m not familiar with what exactly a " ‘build:’ in a template " is. Can you elaborate on that?
Sure.
I was a little of with the file location. It’s actually in the blueprint.
You can see it here.
In my case:
title: Absolvent
pages:
build:
- title: Projektarbeiten
uid: project
num: 1
template: projektarbeit
- title: Abschlussarbeit
uid: abschlussarbeit
num: 2
template: abschlussarbeit
Whenever an ‘Absolvent’ page is created, two other pages are created as subpages. However, when I create this page with the page->create(…) function, it does not create the pages automatically.
Ah, ok, it’s not in the template but the blueprint. Since the page creation takes place outside of the panel, you would have to create these subpages via your hook as well.
An alternative you could try (not tested) is to trigger as panel.page.create
in your hook.
kirby()->trigger('panel.page.create', $page);
and see if that does the job.
Ah, subpage building with blueprints (https://getkirby.com/docs/panel/blueprints/subpages-settings). I have never actually used that - yet 
I just quickly checked the code (of Kirby 2.2.3) and from what I can see there, the subpage builder should actually run right within the create method. Is the subpage builder working when you create the page manually? I am just asking because your blueprint snippet seems to be mixing tabs and spaces and in YAML that is generally not valid (YAML only works with spaces).
This sounds interesting… I never heard of triggers before. Anywhere I could reap up on those? I can’t seem to find anything in the official Docs.
So I would just trigger it as is or do I have to call it on something?
Pretty handy, actually 
Yeah it does. I guess the spaces and tabs are a problem of copying the content out of ST.
I also noticed another strange behavior: When I save the new user, it takes forever to load. It creates the user in a brink and then waits on something. I guess the problem is with the code, but maybe an indication for something…
It’s not in the docs.
You just call it like this:
kirby()->trigger('panel.page.create', $newPage);
where $newPage
is the page you just created.
But as I said, I have never tested it in the context of triggering the blueprint builder.
Ah, sorry, I missed the second argument.
It does increase the creation speed considerably. However, it does not trigger the blueprint-builder.
Any other ideas?^^’
Then I’d just create the two subpages within the hook after the user page has been created as I already suggested in my first post.
So would this work like this?
$newAbsolvent = ...;
$newAbsolvent->children()->create('title', 'projektarbeit', array(...));
$newAbsolvent->children()->create('project', 'projektarbeit', array(...));
$newAbsolvent->children()->create('abschlussarbeit', 'abschlussarbeit', array(...));
I’d put that code after the catch(Exception $e)
bit …
I’d put it inside the try
, otherwise it would still try to create the subpages if creating $newAbsolvent
failed, so the code would be the following:
kirby()->hook('panel.user.create', function($user) {
try {
$newAbsolvent = page('home/absolventen-arbeiten')->children()->create($user->firstName().'-'.$user->lastName(), 'absolvent', array(
'name' => $user->firstName(),
'surname' => $user->lastName(),
'email' => $user->email()
));
$newAbsolvent->children()->create('project', 'projektarbeit', array(...));
$newAbsolvent->children()->create('abschlussarbeit', 'abschlussarbeit', array(...));
} catch(Exception $e) {
echo $e->getMessage();
}
});
I ended up putting it in the try-statement:
try {
$newAbsolvent = page('absolventen-arbeiten')->children()->create( (ucfirst($user->firstName()).' '.ucfirst($user->lastName()) ), 'absolvent', array(
'title' => ucfirst($user->firstName()).' '.ucfirst($user->lastName()),
'name' => $user->firstName(),
'surname' => $user->lastName(),
'email' => $user->email()
));
$newProjekt = $newAbsolvent->children()->create( $user->firstName().' '.$user->lastName()."'s Projekte", 'projektarbeit', array(
'title' => $user->firstName().' '.$user->lastName()."'s Projekte"
));
$newAbschlussarbeit = $newAbsolvent->children()->create($user->firstName().' '.$user->lastName()."'s Abschlussarbeit", 'abschlussarbeit', array(
'title' => $user->firstName().' '.$user->lastName()."'s Abschlussarbeit"
));
kirby()->trigger('panel.page.create', $newAbsolvent);
} catch(Exception $e) {
echo $e->getMessage();
}
It does work this way and another nice side-effect I got out of it, that I can name the site right away. Thanks for the help, guys!
1 Like