Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » Changing Page Defaults

Our old forums are still available as a read-only archive.

Moderators: martimiz, Sean, biapar, Willr, Ingo, simon_w

Page: 1
Go to End
Author Topic: 2465 Views
  • jryancard
    Avatar
    8 Posts

    Changing Page Defaults Link to this post

    Hello! I am trying to set the permissions of a newly created page to the permissions of its parent. I am thinking that I need to override the function populateDefaults(). Is this correct? If so, can I see a small piece of sample code to set the initial values for the fields on the Access tab? Thanks so much.

  • Sean
    Avatar
    Forum Moderator
    921 Posts

    Re: Changing Page Defaults Link to this post

    Hi there,

    First of all, let's take a look at a currently used populateDefaults() method. This is found in the BlogEntry.php file in the blog module.

    /**
    * overload so that the default date is today.
    */
    public function populateDefaults(){
    parent::populateDefaults();
    $this->Date = date("d/m/Y H:i:s",time());
    }

    This does exactly as the doc block above it says.

    So, if we apply what we've seen here to our access restrictions, we could do something like this on your page object:

    class Page extends SiteTree {

    public function populateDefaults() {
    parent::populateDefaults();
    if($parent = $this->Parent()) {
    if($parent->Viewers) $this->Viewers = $parent->Viewers;
    if($parent->Editors) $this->Editors = $parent->Editors;
    if($parent->ViewersGroup) $this->ViewersGroup - $parent->ViewersGroup;
    if($parent->EditorsGroup) $this->EditorsGroup = $parent->EditorsGroup;
    }
    }

    }

    Stepping through, this code checks if the page has a parent, then it checks each of the access permission fields, and if they exist on the parent page. If they do, it then assigns them to the current page fields.

    You can see the fields we've manipulated on the SiteTree table.

    In theory, this is how it should work.

    Hope this helps!

    Sean

  • jryancard
    Avatar
    8 Posts

    Re: Changing Page Defaults Link to this post

    Hey Sean, thanks for the help!

    Unfortunately, that did not work. I had found the populateDefaults() function in the BlogEntry class, and that's what made me think that same idea would work for setting the defaults of page permissions based on its parent's permissions.

    The fields in the Access tab did not default to parent permissions, and, in addition, I got an Ajax error when I tried to save and publish the page with this function present.

    I remember seeing on the forums somewhere that newly created pages are not inserted into the database until actually saved, so the page doesn't have actually have a parent, at least relative to the database, upon creation. Is there another way to set the Access tab fields to the same as its parent's on page creation? Thanks again!

  • Sean
    Avatar
    Forum Moderator
    921 Posts

    Re: Changing Page Defaults Link to this post

    Hi there,

    Unfortunate that it didn't work. I see what you're saying, since the page hasn't actually been created yet.

    It's only a guess, but you could try using onBeforeWrite() or onBeforeSave(). I'd suggest the first one, and then try the next one. They should do roughly the same thing. So, you could just substitute the populateDefaults with one of the aforementioned methods.

    Hope this helps!

    Sean

  • jryancard
    Avatar
    8 Posts

    Re: Changing Page Defaults Link to this post

    Sean, thanks for helping me out on this!

    After a little modification to check whether the page is a record in the database, here is the code that allows me to inherit permissions from the parent; I placed this in my Page.php. I also remove the Access tab so that it doesn't confuse the user when permissions are saved. Somehow, I need to redisplay it once it is written to the database, but that's a smaller issue for me right now. Thanks again!

       function getCMSFields() {
          $fields = parent::getCMSFields();
          if (!$this->exists()) {
             $fields->removeByName("Access");
          }
          
          return $fields;
       }
       
       function onBeforeWrite() {
          //If the page already exists in the database, don't take parent permissions
          if (!$this->exists()) {
             if($parent = $this->Parent()) {
                if($parent->Viewers) $this->Viewers = $parent->Viewers;
                if($parent->Editors) $this->Editors = $parent->Editors;
                if($parent->ViewersGroup) $this->ViewersGroup = $parent->ViewersGroup;
                if($parent->EditorsGroup) $this->EditorsGroup = $parent->EditorsGroup;
             }
          }
          parent::onBeforeWrite();
       }

    2465 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.