Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Archive /

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

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

Changing Page Defaults


Reply


5 Posts   2524 Views

Avatar
jryancard

8 Posts

25 October 2007 at 9:46am

Edited: 25/10/2007 9:47am

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.

Avatar
Sean

Forum Moderator, 922 Posts

27 October 2007 at 2:05am

Edited: 27/10/2007 2:10am

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

Avatar
jryancard

8 Posts

27 October 2007 at 8:06am

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!

Avatar
Sean

Forum Moderator, 922 Posts

27 October 2007 at 3:24pm

Edited: 27/10/2007 3:27pm

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

Avatar
jryancard

8 Posts

30 October 2007 at 5:51am

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();
   }