Jump to:

5450 Posts in 1672 Topics by 1197 members

Customising the CMS

SilverStripe Forums » Customising the CMS » page limit

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

Page: 1
Go to End
Author Topic: 1492 Views
  • Rajaneesh
    Avatar
    Community Member
    3 Posts

    page limit Link to this post

    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"

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: page limit Link to this post

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

  • Juanitou
    Avatar
    Community Member
    322 Posts

    Re: page limit Link to this post

    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

  • banal
    Avatar
    Community Member
    901 Posts

    Re: page limit Link to this post

    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

  • Juanitou
    Avatar
    Community Member
    322 Posts

    Re: page limit Link to this post

    Thank you!

  • Rajaneesh
    Avatar
    Community Member
    3 Posts

    Re: page limit Link to this post

    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 ....

  • Four
    Avatar
    Community Member
    6 Posts

    Re: page limit Link to this post

    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?

  • banal
    Avatar
    Community Member
    901 Posts

    Re: page limit Link to this post

    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?

    1492 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.