Programmable blueprints

Hello,

I looked at the “Programmable blueprints” page:

<?php

use Kirby\Cms\App as Kirby;

Kirby::plugin('cookbook/programmable-blueprints', [
    'blueprints' => [
        'site' => function () {
            if (($user = kirby()->user()) && $user->isAdmin()) {
                return Data::read(__DIR__ . '/blueprints/site.admin.yml');
            } else {
                return Data::read(__DIR__ . '/blueprints/site.editor.yml');
            }
        },
    ]
]);

And in the following example I wanted to adapt the code to have 4 different types of users.

How to adapt the code to more than 2 types of users?

Hello,

You can do $user->name() and $user->username(), see the Docs.

You could also do $user->role() if your users are define by extra roles, or use any of $user methods that fits your case.

With that you can add more if/else clauses to that block, to match each of your users.

Of course your users must exist.

Does this help ?

Hi plagasul,

I tried something like this but I got an error on the login page:

<?php

use Kirby\Cms\App as Kirby;

Kirby::plugin('cookbook/programmable-blueprints', [
    'blueprints' => [
        'site' => function () {
            if (($user = kirby()->user()) && $user->isAdmin()) {
                return Data::read(__DIR__ . '/blueprints/site.admin.yml');
            }
            else if(($user = kirby()->user()->role()) === 'editor'){
                return Data::read(__DIR__ . '/blueprints/site.editor.yml');
            }
            else if(($user = kirby()->user()->role()) === 'editor_stories'){
                return Data::read(__DIR__ . '/blueprints/site.editor_stories.yml');
            }
            else if(($user = kirby()->user()->role()) === 'circuits'){
                return Data::read(__DIR__ . '/blueprints/site.circuits.yml');
            }
        },
    ]
]);

Error: /kirby/config/areas/site.php

<?php
 
use Kirby\Toolkit\I18n;
 
return function ($kirby) {
    return [
        'breadcrumbLabel' => function () use ($kirby) {
            return $kirby->site()->title()->or(I18n::translate('view.site'))->toString();
        },
        'icon'      => 'home',
        'label'     => $kirby->site()->blueprint()->title() ?? I18n::translate('view.site'),
        'menu'      => true,
        'dialogs'   => require __DIR__ . '/site/dialogs.php',
        'dropdowns' => require __DIR__ . '/site/dropdowns.php',
        'searches'  => require __DIR__ . '/site/searches.php',
        'views'     => require __DIR__ . '/site/views.php',
    ];
};

the problematic line is: 'label' => $kirby->site()->blueprint()->title() ?? I18n::translate('view.site'),

If I add an else it works but it always falls into the else.

  1. in your elseifs, you don’t check for an existing user
  2. role returns a role object, not a string, so your comparision will fail
  3. you have to return a default if your ifs fail

I fixed like this and everything works.

Is this a good solution?

<?php

use Kirby\Cms\App as Kirby;

Kirby::plugin('cookbook/programmable-blueprints', [
    'blueprints' => [
        'site' => function () {

            // Si l'utilisateur est deconnecté
            if(!kirby()->user()){
                return Data::read(__DIR__ . '/blueprints/site.circuits.yml');
            };

            // Si l'utilisateur est connecté
            if (($user = kirby()->user()) && $user->isAdmin()) {
                return Data::read(__DIR__ . '/blueprints/site.admin.yml');
            }
            else if(($user = kirby()->user()->role()->name() === 'editor')){
                return Data::read(__DIR__ . '/blueprints/site.editor.yml');
            }
            else if(($user = kirby()->user()->role()->name()) === 'editor_stories'){
                return Data::read(__DIR__ . '/blueprints/site.editor_stories.yml');
            }
            else if(($user = kirby()->user()->role()->name()) === 'circuits'){
                return Data::read(__DIR__ . '/blueprints/site.circuits.yml');
            }
            // else {
            //     return Data::read(__DIR__ . '/blueprints/site.circuits.yml');
            // }
        },
    ]
]);

That is a bit repetitive, and can be shortened to:

Kirby::plugin('cookbook/programmable-blueprints', [
    'blueprints' => [
        'site' => function () {
            $user = kirby()->user();
            // Si l'utilisateur est deconnecté
            if(! $user || ! in_array($user->role()->name(), ['admin', 'editor', 'editor_stories'])) {
                return Data::read(__DIR__ . '/blueprints/site.circuits.yml');
            }

            // Si l'utilisateur est connecté
            return Data::read(__DIR__ . '/blueprints/site.' . $user->role()->name() . '.yml');
       
 
        },
    ]
]);

(since writing without editor, I might have missed some parenthesis…)

Thanks, is perfect!

@texnixe dear sonja, could it be that this does not work for user blueprints? i have a project where it works perfectly for pages, for this new project i needed it for users and the panel crashes somehow. can you please confirm this?