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.

Customising the CMS /

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

I wanna make a sidebar, but how?


Go to End


37 Posts   20084 Views

Avatar
martin_w

Community Member, 28 Posts

11 January 2009 at 5:35am

Hi guys,

at first.. I startet yesterday working with silverstripe and its really great, good job!

Now my question.
I wanna make a sidebar on my website which can be edit in admin.
I found no module for it so I wanna make it in this way:
I wanna make a invisible page (called "sidebar") in admin and include it in my sidebar.. But I dont know which code I would need to include.. and if it would work at all.

I hope someone can help and understand (I am from germany) me.

p.s. I know there is a sidebar within the blog module, but I dont want this sidebar.

Avatar
Carbon Crayon

Community Member, 598 Posts

11 January 2009 at 9:49am

Edited: 30/03/2009 2:33am

[EDIT]

There is now a tutorial on creating a sidebar on SSbits.com
http://www.ssbits.com/create-a-static-sidebar-editable-from-the-cms/

[/EDIT]

Hi Martin, welcome to silverstripe :)

Here is what you need to do:

First create a class for your Links. We need to make the links objects so that you can then add as many as you want to your page.

So your code for the Links class might look something like this:

<?php

class Link extends DataObject {

	static $db = array(
		'Link' => 'Varchar(255)',
                'LinkText' => 'Varchar(255)',
	);

     static $has_one = array(
		'LinksPage' => 'LinksPage',
	);

    function getCMSFields_forPopup() {
		return new FieldSet(
			new TextField('Link'),
			new TextField('LinkText')
  
		);

	}
	
   
}
?>

Notice the '$has_one' variable. This is to tell SS that the each link will be connected to a LinksPage.

Next we create our Links page type in which you will be able to control your links from:

class LinksPage extends Page {

	static $db = array(	
		
	);
	
	static $has_many = array(
		'Links' => 'Link',
	);	

function getCMSFields() {
		$fields = parent::getCMSFields();
		
		$LinkField = new HasManyComplexTableField(
			$this,
			'Links',
		    'Link',
		    array(
			    'Link' => 'Link',
			    'LinkText' => 'LinkText',
		    ),
		    'getCMSFields_forPopup',
	);
		$LinkField->relationAutoSetting = true;
		$LinkField->setAddTitle( 'A Link' );
		$fields->addFieldToTab( 'Root.Content.Links', $LinkField );
 
                //you wont need any content on your sidebar so lets remove it's field
               $fields->removeFieldFromTab("Root.Content.Main","Content"); 
		return $fields;	
	}

}

class LinksPage_Controller extends Page_Controller{	
	
}

?>

Then in your Page_Controller you can create a function that will get your links and return them to the template:


function GetLinks(){
   $LinksPage = DataObject::get_one("LinksPage");
   return ($LinksPage) ? DataObject::get("Link", "LinksPageID = {$LinksPage->ID}") : false;
}

Finally in your SideBar.ss include you can now call <% Control GetLinks %> which will return all your links.

Hope that helps, any questions just ask :)

Aram

Avatar
martin_w

Community Member, 28 Posts

11 January 2009 at 10:47am

Hi aram,

thank you for your answer.
I think its not exactly that what I want.
I wanna put in my sidebar not only links, also some informations (text) and maybe images. So, thats why I would like to change it like the pages.

Avatar
Carbon Crayon

Community Member, 598 Posts

11 January 2009 at 11:39am

Edited: 11/01/2009 11:41am

so you need to change it on a per-page basis? or you just want to add images etc to it that will appear on every page?

if its going to look the same on every page then its eays all you have to do is add those fields into the LinksPage class. Perhaps that was a bad name for it, it should have been called SideBarPage because it contains all of the items you want in the sidebar, which includes a list of links.

So instead of taking out the 'content' field you can leave that in and use it as normal, add some image fields etc.

then in your Page_Controller we can just use a function 'GetSidebar' which will return the entire sidebar page object so we can access all of the items on it:

function GetSidebar {
   return DataObject::get_one("SidebarPage");
}

then in the Sidebar.ss template we can just do something like this:

<% control GetSidebar %>

$Content

$Image

<% control Links %>

<a href="$Link" >$LinkText</a>

<% end_control %>
<% end_control %>

You should have full flexibility to treat the SidebarPage (previously LinksPage) just like a normal page adding any fields/tabs etc you like :)

Avatar
martin_w

Community Member, 28 Posts

11 January 2009 at 11:37pm

yeah it shall appear on every page.

But I still have a few questions (about the codes).
The first question is, if I need for your function "GetSidebar" also the codes from your post before?
The other question is, if your first code which has the class "Link extends DataObject" needs to be in a own file like "sidebar.php" or so (which is in /code/).
and my last question.. I acutally want this sidebar without links.. just with content like I said.. images, text and so. Can you tell me how the code has to look then?

I am really thankful for your help.

Avatar
Carbon Crayon

Community Member, 598 Posts

12 January 2009 at 12:06am

Edited: 12/01/2009 1:37am

your welcome :) I seem to have totally misunderstood and not read your initial post properly, sorry about that! hehe

here is the code for what you want:

mysite/code/SideBarPage.php

class SidebarPage extends Page {

   static $db = array(   
              'SidebarText' => Text;
   );
   
   static $has_one = array(
      'Image1' => 'Image',
      'Image2' => 'Image',
      'Image3' => 'Image',
   );   

function getCMSFields() {
      $fields = parent::getCMSFields();  

$fields->addFieldToTab('Root.Content.Main', new TextAreaField('SidebarText'), 'Content');
$fields->addFieldToTab("Root.Content.Images", new ImageField('Image1'),'Content');
$fields->addFieldToTab("Root.Content.Images", new ImageField('Image2'),'Content');
$fields->addFieldToTab("Root.Content.Images", new ImageField('Image3'),'Content');

      return $fields;   
   }

}

class SidebarPage_Controller extends Page_Controller{   
   
}
?>

mysite/code/Page.php

class Page_Controller extends ContentController{
	
public function GetSidebar(){
return DataObject::get_one("SidebarPage");
}
	
}

themes/yourTheme/templates/Includes/Sidebar.ss

<% control GetSidebar %>

$SidebarText

$Image1

$Image2

$Image3

<% end_control %>

That should do it :)

Avatar
martin_w

Community Member, 28 Posts

12 January 2009 at 12:26am

I have done what you wrote, but I get a white page now. and "Debug::send_errors_to" sends no error mail to me.

But I am sure now, that is what I wanted. :)

and by the way.
you've forgotten 2 ' in this code part:

static $has_one = array(
'Image1' => 'Image',
'Image2' => 'Image',
'Image3' => 'Image',
); 

(but that was not the error why I get a white page).

Avatar
Carbon Crayon

Community Member, 598 Posts

12 January 2009 at 12:43am

Edited: 12/01/2009 12:43am

oops,sorry for the typo, fixed now :)

Just to be clear, you cant actually navigate the the SidebarPage and see anything because we havnt defined a SidebarPage.ss template. Instead we are just grabbing some items from it to include in our Sidebar.ss include. So inorder to see anything you have to <% include Sidebar %> inside another page template.

If you still have trouble let me know

Go to Top