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"
This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.
Please use forum.silverstripe.org for any new questions
(announcement).
The forum archive will stick around, but will be read only.
You can also use our Slack channel
or StackOverflow to ask for help.
Check out our community overview for more options to contribute.
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"
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;
}
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
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
Thank you!
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 ....
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?
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?