Hiding Pages

Silverstripe Version: 4

Question: How can I hide pages from the CMS?

So, I’d like to hide a page or 2 from the CMS… This used to be easy enough in SS 3 via data extensions, but I’m not having any success in SS 4. Here’s where I’m at…

// For my extension
namespace MyApp\Web\Extensions;

use SilverStripe\ORM\DataExtension;

class CantCreateExtension extends DataExtension
{

    public function canCreate($member = null, $context = [])
    {
        return false;
    }

}

// For my config
VirtualPage:
  extensions:
    - MyApp\Web\Extensions\CantCreateExtension 

Am I on the right track? Any suggestions?

@CW_Chong I also just tried a new custom page… Am I on the right track with something like this?

private static $hide_ancestor = 'SilverStripe\CMS\Model\VirtualPage';

public function canCreate($member = null, $context = [])
{
    return false;
}

Hi Andrew, checking back on the source of SiteTree, it does support extending canCreate, so your original solution works, along with adding namespace “SilverStripe\CMS\Model\VirtualPage” in yml as you quoted above.

The only thing I did in addition is to use “dev/build/?flush=1” after;
I recall encountering this issue before, whereby the “Add new” in the menu doesn’t update unless a flush is made; try it.

Thanks for all the help! I couldn’t get it working with the dataextension, but I did get it working creating the custom hide pages… Feels super hacky, but works, so I’m good for now. Would be sweet if we could do something simple and declarative in the yml.

You’re on the right track. What you’ve seemed to have missed is to properly namespace VirtualPage in your config yml.

I might be misunderstanding but i thought you will need a subclass (with usecustomclass or yml injector) to override “canCreate”?

Is it available as a hook on dataextension?

I tried this, but this doesn’t work…

SilverStripe\CMS\Model\VirtualPage:
extensions:
- MyApp\Web\Extensions\CantCreateExtension

Hi @andrewhoule,

SiteTree::canCreate() also calls canCreate on any dataextensions – if any return false, this counts as a ‘veto’ and the user shouldn’t be able to create that pagetype. It could also be that you’re logged in as ADMIN, in which case I think any can* just returns true (ADMIN can do anything).

public function can($perm, $member = null, $context = array())
{
    // ...
    if ($member && Permission::checkMember($member, "ADMIN")) {
        return true;
    }
    // (can-methods on extensions never get called for ADMINS)

So for editors (non-ADMINs) your extension approach should work, if you think somehow your extension doesn’t get applied/loaded, I’d throw in a quick die(‘YUP’); in CantCreateExtension::canCreate(). Then if that doesn’t get called check if your namespaces are correct and make sure to run ?flush=1 to update the yaml configs.

As for ‘something simple and declarative in the yml’, I just noticed the below config setting in SiteTree, might be a way to achieve what you’re after (remember ADMINs can do anything).

/**
 * List of permission codes a user can have to allow a user to create a page of this type.
 * Note: Value might be cached, see {@link $allowed_chilren}.
 *
 * @config
 * @var array
 */
private static $need_permission = null;