Set page author automatically when a page is created

I am (was) using a page.create:after hook to set the author of a page to the current user …

'page.create:after' => function ($page) {
      if ($this->user()) {
        try {
          $page->update([
            'author' => $this->user()->email()
          ]);
        } catch(Exception $e) {
          echo $e->getMessage();
        }
      }
    }

The author field looks like this:

author:
  label: Author
  type: users
  multiple: false
  disabled: true

This code worked (at least for some beat versions) but does not work anymore!? I know there was a bug for disabled fields but even without disabled: true it does not work.

When I change this line

'author' => $this->user()->email()

to

'text' => $this->user()->email()

the textfield of the page is filled with kirby@getkirby.com. So the hook itself works, but $site->user() does not return the current user and the users field does not accept the value?!

Any ideas?

I found a solution. It works with page.create:before and Yaml::encode() like this …

  'page.create:before' => function ($page, $input) {
      if ($this->user()) {
        try {
          $page->update([
            'author' => Yaml::encode([$this->user()->email()])
          ]);
        } catch(Exception $e) {
          echo $e->getMessage();
        }
      }
    }

Are we able to use queries in default: values? If we are, then we should be able to do something like this:

author:
  label: Author
  type: users
  multiple: false
  disabled: true
  default: "{{site.kirby.user}}" # or site.kirby.user, not sure...
1 Like

I tried several combinations but unfortunately it doesn’t work. Nevertheless the idea is great!

I just tested this again, and if you don’t explicitly set a default value (or set default: null), Kirby automatically picks the current user.

If you want to explicitly set any users:

author:
  type: users
  default: 
    - you@example.com
    - me@example.com

(those user email addresses have to exist)

If you don’t want a default user, you can set default: false.

3 Likes

Damn, I didn’t know that is so easy! I set up the hook without checking the default behaviour and the hook disabled the default from the beginning. :man_facepalming:

I removed the hook and everything works as described. Thanks @texnixe!

It’s probably because - as far as I remember - there was a bug earlier in the develop phase, and you had to use the hook instead.

Oh yes, this is probably the reason. This was my dev project since the early alpha versions of K3.

Am I right that according to your post (24th January), the example below should set the default value to the currently logged in user @texnixe?

  author:
    type: users
    required: true
    default: none

The default: none part doesn’t work like that in the latest release. If I remove it, the current user is set properly due to the required part. If this is a bug, I can open an issue on Github.

none is not a valid value, I didn’t suggest that.

If you want to use the current user as default, do not set a default at all (because the current user is the default) or set default: null.

If you don’t want to set a default user, set default: false.

Note that defaults only work for newly created pages.

1 Like

. . . thanks for your quick clarification. Seems like I have misread null for none in the night . . .

Works as expected!