Set a custom field definition to readonly

Hi,

I try to build a custom field that is readonly for all users except one defined in another field of the same page. I tried to do this in the constructor like this:

class ApprovecontentField extends SelectField {

  public function __construct() {

    $this->type = 'select';

    // Get the defined user with access
    $designatedApprover = $this->page()->contentapprover();
    $currentUser = kirby()->site()->user()->username();

    // Lock the field if user is not the one set for approval
    $this->readonly = $currentUser == $designatedApprover ? false : true;

    $this->options = array();
    $this->icon    = 'chevron-down';
  }

}

However it seems I have no access to $this->page() inside the constructor. What is the best way to achieve this?

Kind regards,
Georg

I wouldn’t touch the constructor and only add a readonly method:

  public function readonly() {
    $designatedApprover = $this->page()->contentapprover();
    $currentUser = kirby()->site()->user()->username();
    if($currentUser == $designatedApprover) {
      return false;
    } else {
      return true;
    }
  }

Then the rest of the custom field can be left as it is.

Thank you! That was exactly what I was looking for!