Reputation: 1914
Imagine a site structure / menu layout like this:
Home
About us
Services
__Peeling potatoes
__Slicing potatoes
__Baking potatoes
All menu items link to a real page, with their own URL and content. But the bolded item is merely a menu item without a link, content nor URL and it's only purpose is to fold out the submenu on hover. SilverStripe doesn't allow you to create such a Page entity out-of-the-box.
I'm looking for the cleanest, most simple and least hacky way to create a dummy page merely to function as a menu item, no content and in the best case also without a URL slug (the latter might be difficult).
Upvotes: 2
Views: 136
Reputation: 5875
You can achieve the "dummy" page without any additional code, by just creating a RedirectorPage
and select your first sub-page as redirection target.
Personally, I've used a even simpler version of a "RedirectorPage" in the past, one that automatically redirects to the first child-page if it were visited directly.
Example:
class ChildRedirectorPage extends Page
{
private static $description = 'Page that has no content but automatically redirects to the first of its child-pages';
public function ContentSource() {
if($firstChild = $this->Children()->First()) {
return $firstChild;
} else {
return $this;
}
}
public function Link($action = null) {
// prevent link "redirection" when we're in CMS
if (!is_subclass_of(Controller::curr(),'LeftAndMain')){
if($firstChild = $this->Children()->First()) return $firstChild->Link($action);
else return parent::Link($action);
}
}
public function getCMSFields() {
$fields = parent::getCMSFields();
$fields->removeByName('Content', true);
return $fields;
}
}
class ChildRedirectorPage_Controller extends Page_Controller
{
function init() {
parent::init();
if($firstChild = $this->Children()->First()) {
$this->redirect($firstChild->Link(), 301);
}
}
}
I think the URL slug is actually beneficial, since your URLs will then be services/peeling-potatoes
, etc. which is most likely better for SEO purposes.
Upvotes: 3