Creating a new page programmatically with a users field

Hi,

I am using Page::create to generate a new page in Kirby. One of the fields in this page blueprint is a users field. When I generate the page I would like this to be blank, but currently it is filled in with - user://kirby. I guess this is because I am impersonating Kirby as part of the page creation.

What is the best way of getting this field to be blank when I create the page? I have tried setting it to null in the content section of Page::create but it hasn’t worked

Try to set it to an empty array. If that does not work, please post your blueprint.

Hi,
Thanks for the suggestion, unfortunately it didn’t work this time.

This is my blueprint (it is rather large so this is just the relevant section - the users field in question is mentor at the bottom):

title: School
options:
  changeSlug: false
  duplicate: false
  preview: false
  delete:
    admin: true
    mentor: false
  changeStatus:
    admin: true
    mentor: false
  changeTitle:
    admin: true
    mentor: false
tabs:
  informationTab:
    label: Information
    icon: user
    sections:
      information:
        type: fields
        fields:
          informationHeadline:
            label: Information
            type: headline
            help: This is the general information provided by the school, and as such shouldn't need updating
            numbered: false
          schoolType:
            type: select
            label: School type
            width: 1/3
            options:
                primary: Primary
                secondary: Secondary
                alternative: Alternative provision
          schoolDirectLine:
            width: 1/3
            type: tel
            label: School direct line
          mobileNumber:
            width: 1/3
            type: tel
            label: Mobile number
          contactFirstName:
            width: 1/4
            type: text
            label: Contact first name
          contactSurname:
            width: 1/4
            type: text
            label: Contact surname
          contactEmail:
            width: 1/4
            type: email
            label: Contact email
          contactRole:
            width: 1/4
            type: text
            label: Contact role
          logo:
            label: School logo
            help: This is used when the school posts in the Community forum
            layout: cards
            width: 1/4
            type: files
            multiple: false
            size: small
            query: page.images
            uploads: school-logo
            template: school-logo
            image:
              back: white
              ratio: 1/1
          mentor:
            type: users
            multiple: false
            query: kirby.users.filterBy('role','in', ['mentor','admin'])
            empty: No mentor selected
            image: false

This is the page creation setup as well - it is inside a Uniform action, but again I have just pulled out the relevant bit. This is with your suggestion of the empty array:


try {
            kirby()->impersonate('kirby');
            $page = Page::create([
                'slug'     => $slug,
                'template' => 'school',
                'content' => [
                    'title'  => $name,
                    'schoolType' => $schoolType,
                    'contactFirstName' => $contactFirstName,
                    'contactSurname' => $contactSurname,
                    'contactRole' => $contactRole,
                    'schoolDirectLine' => $schoolDirectLine,
                    'mobileNumber' => $mobileNumber,
                    'contactEmail' => $email,
                    'accessApproved' => false,
                    'mentor' => [],
                  ]
              ]);

            $page->changeStatus("unlisted");
            kirby()->impersonate(null);

        } catch (\Exception $e) {
            $this->fail($e->getMessage());
        }

Had to do some testing, but the only thing that worked to keep the users field empty, is by setting default: false. Wondering if this is a bug, because the default should be no users selected.

Thanks for your work on this - I have added default: false to the blueprint which is doing the job. As you say, I think this should be the default when a new page is created with a users field.