Remove 'Duplicate this page and subpages' functionality in Silverstripe
Asked Answered
A

2

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?

Agility answered 29/8, 2016 at 4:56 Comment(0)
A
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();
}
Agility answered 7/9, 2016 at 23:55 Comment(0)
S
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'
Sharpfreeze answered 29/8, 2016 at 21:24 Comment(4)
See SiteTree::canAddChildren() in order to restrict creation of child pagesStithy
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.Sharpfreeze
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 SS4Stithy
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.Agility
A
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();
}
Agility answered 7/9, 2016 at 23:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.