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.

Customising the CMS /

page limit


Reply


8 Posts   1624 Views

Avatar
Rajaneesh

Community Member, 3 Posts

6 August 2009 at 10:09pm

Please help me .. i am new user for silverstripe .

i want limit a page upto 10 pages .. after that when user try to create new page it should trough a error message that "Access Denied"

Avatar
Willr

Forum Moderator, 5513 Posts

9 August 2009 at 11:50am

You can create a function called canCreate() which has the permission if a user can create a given page. If you want this check to be over the whole site you would do something like this on your Page.php file in the Page class

function canCreate() {
$pages = DataObject::get('SiteTree');
return ($pages && $pages->Count() > 10) ? false : true;
}

Avatar
Juanitou

Community Member, 323 Posts

10 August 2009 at 7:26am

Hi Will!

Maybe I'm wrong, but I think that it's worth mentioning that by creating this function we will overload (right term?) the canCreate() function present in DataObject. Isn't it?

Best regards,
Juan

Avatar
banal

Community Member, 901 Posts

10 August 2009 at 8:21am

Edited: 10/08/2009 8:23am

Yes, doing so, you will indeed overload the canCreate method (not of DataObject, but of SiteTree). But that isn't really a problem.
If you do user-permission checks in the canCreate method of a base-class, you can catch this first:

function canCreate($member = null) {
   // check permission of base-class
   $permission = parent::canCreate($member);
   // if there is no permission, we don't grant it here either.
   if($permission === false)
      return false;

   $pages = DataObject::get('SiteTree');
   return ($pages && $pages->Count() > 10) ? false : true;
}

That way, you can create up to 10 SiteTree instances, but only if you have the permissions to do so

Avatar
Juanitou

Community Member, 323 Posts

10 August 2009 at 9:39am

Thank you!

Avatar
Rajaneesh

Community Member, 3 Posts

10 August 2009 at 4:40pm

thank for reply ..
but show me how to do in before tiger i want call that function in addpage() function ? CMSMain.php

it should send error message and i want display that message in CMSLeft.ss

please help me ....

Avatar
Four

Community Member, 6 Posts

27 August 2009 at 9:37pm

following on from this, how would I only limit the number of Level1 (top-level) pages that can be created without placing a limit on Level2 pages?

Avatar
banal

Community Member, 901 Posts

27 August 2009 at 11:31pm

Edited: 27/08/2009 11:35pm

Hi. That should be simple to implement. Use the canCreate method, listed above, but change the following:

$pages = DataObject::get('SiteTree', 'ParentID = 0');

The WHERE statement checks if ParentID is zero, which is only the case for top-level pages.

Update: Sorry, my solution is slightly flawed. The limit check will only occur on Level 1 Pages, but you won't be able to create sub-pages once that limit is reached. Sadly, I don't know an easy work-around for your problem, you would have to find out what level the currently selected item of the SiteTree Navigation belongs to. Maybe somebody knows a easy way to check this?