Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Blog Module /

Discuss the Blog Module.

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

remove BlogHolder as a viewable page


Go to End


2 Posts   1852 Views

Avatar
Seriaph

Community Member, 1 Post

23 February 2016 at 1:57pm

Edited: 23/02/2016 2:00pm

Hi All,

I couldn't find any help on google/forums similar to my situation, so here goes.

My current site structure looks something like this:

  • BlogTree

    • BlogHolder

      • BlogEntry
      • BlogEntry
      • BlogEntry
      • BlogEntry

    • BlogHolder

      • BlogEntry
      • BlogEntry
      • BlogEntry
      • BlogEntry

    • BlogHolder

      • BlogEntry
      • BlogEntry
      • BlogEntry
      • BlogEntry

I'm hoping to achieve two things:

  • If you access BlogEntry, the breadcrumb to BlogHolder will not be a link. You can't access the page.
  • If you manually type in the link to a BlogHolder page, it will either redirect to the first article in the category, or 404.

I'm new to silverstripe, so if anybody knows of better solutions, I'd love to hear them.

Avatar
Sygmoral

Community Member, 46 Posts

21 April 2016 at 3:31pm

Edited: 21/04/2016 3:37pm

You can achieve this using Extensions. The Blog object itself does not need to be extended, but the controller for it does (the part that handles where URLs go).

So in your mysite/_config/config.yml, write something like:

Blog_Controller:
  extensions:
    - BlogControllerExtension

This is assuming you use the latest version of the Blog module, 2.0, which has the classes Blog and BlogPost (and matching _Controllers).

Then in your code you'd have a file BlogControllerExtension.php with something like this:

class BlogControllerExtension_Controller extends Blog_Controller {
	function init() {
		parent::init();
		$blog = $this->getOwner();
		
		// If there is only one blog post, redirect to it
		if($blog->getBlogPosts()->count()==1) {
			return $this->redirect($blog->getBlogPosts()->First()->AbsoluteLink());
		}
		// If there are no blog posts, redirect to homepage
		else if(!$blog->getBlogPosts()->count()) {
			return $this->redirect('/'); 
		}
	}
}

Well, something like that should work :)