Panel field not updated after page.update:after hook

Hi everyone,
I use the page.update.after hook to restrict a field based on user’s role.
Here is my hook :

       'page.update:after' => function (Kirby\Cms\Page $newPage, Kirby\Cms\Page $oldPage) {
        /**
         * Externe role can't edit gestionnaire field
         * @var \Kirby\Cms\Role $role
         */
        $role = $this->user()->role();
        if ($role->name() === 'externe' && $oldPage->gestionnaire()->exists()) {
            $newPage->update([
                'gestionnaire' => $oldPage->gestionnaire()->value()
            ]);
        }

    }

The field is saved correctly, but the panel shows the wrong value. It show the good one only after refreshing (F5) the panel.

The hook runs on the server side, therefore changes on the backend are not reflected on the frontend without reload.

What you could do instead is to create conditional blueprints and disable the field for certain roles:

This example is for complete blueprints, but can also be done for fields.

Example:

<?php
$role = kirby()->user()->role();
$fields = [
    'gestionnaire' => [
        'type' => 'textarea',
        'disabled' => $role->name() === 'external' ? true : false,
    ],
    // more fields here
];

$yaml = [
    'title'   => 'Blueprint',
    'columns' => [
        'main' => [
            'width'    => '2/3',
            'sections' => [
                'fields' => [
                  'type'     => 'fields',
                  'fields' => $fields,
                  ]
              ],
        ]
    ],
    // more stuff

];

return $yaml;
1 Like

Oh, i didn’t found that feature. Really nice.
Thanks a lot :slight_smile:

If the field should be hidden from a given role, you can also make it hidden instead of disabling it.

1 Like