7

In Silverstripe if you right click on a page in Sitetree you have the ability to duplicate either a single page or a page and all its children.

enter image description here

We have found that users occasionally duplicate pages with large numbers of children and would like to prevent this by either removing the 'This page and subpages' option or by restricting it to admin users only.

How can this be achieved?

jamckp
  • 207
  • 2
  • 11

2 Answers2

3

Looking at the code in cms/javascript/CMSMain.Tree.js in SilverStripe 3.4 it doesn't look like there is currently a way to switch this off.

One option we have is to add some CSS to the CMS to hide the menu item for everybody:

mysite/css/cms.css

#vakata-contextmenu a[rel="duplicate"] + ul > li:last-child {
    display: none;
}

To enable the cms.css file we add the following line to our config.yml

mysite/_config/config.yml

LeftAndMain:
  extra_requirements_css:
    - 'mysite/css/cms.css'
3dgoo
  • 15,716
  • 6
  • 46
  • 58
  • See SiteTree::canAddChildren() in order to restrict creation of child pages – Greg Smirnov Sep 05 '16 at 11:55
  • 1
    We still want to be able to add children pages. We just want to remove the _duplicate page and supages_ option. Using `SiteTree::canAddChildren()` will prevent being able to create children pages completely. – 3dgoo Sep 05 '16 at 12:01
  • 1
    You are right... then it is a bit more complex. try to throw exception in Page::onBeforeDuplicate() to restrict access, or overload SiteTree::duplicateWithChildren(). I agree, we need to make it easier in SS4 – Greg Smirnov Sep 05 '16 at 12:26
  • Page::onBeforeDuplicate and SiteTree::duplicateWithChildren both seem to be viable solutions. I'll post the code for the final solution tomorrow when I have a chance to write it. – jamckp Sep 06 '16 at 04:18
0

Adding the following code to page.php prevents non admin users from duplicating pages and subpages. The menu item is still visible which is suboptimal but is good enough as a quick solution.

public function duplicateWithChildren() {
    if(!Permission::check('ADMIN')) {
        throw new ValidationException("You must be logged in as an Admin to duplicate a page and subpages");
    }
    return parent::duplicateWithChildren();
}
jamckp
  • 207
  • 2
  • 11