Jump to:

5541 Posts in 1738 Topics by 1224 members

Customising the CMS

SilverStripe Forums » Customising the CMS » I wanna make a sidebar, but how?

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

Page: 1 2 3 4 5
Go to End
Author Topic: 13011 Views
  • martin_w
    Avatar
    Community Member
    28 Posts

    I wanna make a sidebar, but how? Link to this post

    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.

  • Aram
    Avatar
    Community Member
    598 Posts

    Re: I wanna make a sidebar, but how? Link to this post

    [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

  • martin_w
    Avatar
    Community Member
    28 Posts

    Re: I wanna make a sidebar, but how? Link to this post

    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.

  • Aram
    Avatar
    Community Member
    598 Posts

    Re: I wanna make a sidebar, but how? Link to this post

    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

  • martin_w
    Avatar
    Community Member
    28 Posts

    Re: I wanna make a sidebar, but how? Link to this post

    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.

  • Aram
    Avatar
    Community Member
    598 Posts

    Re: I wanna make a sidebar, but how? Link to this post

    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

  • martin_w
    Avatar
    Community Member
    28 Posts

    Re: I wanna make a sidebar, but how? Link to this post

    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).

  • Aram
    Avatar
    Community Member
    598 Posts

    Re: I wanna make a sidebar, but how? Link to this post

    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

    13011 Views
Page: 1 2 3 4 5
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.