Jump to:

791 Posts in 369 Topics by 303 members

Widgets

SilverStripe Forums » Widgets » [Solved] How to add widgets in the every site

Discuss SilverStripe Widgets.

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

Page: 1 2
Go to End
Author Topic: 3403 Views
  • funfried
    Avatar
    Community Member
    22 Posts

    Re: [Solved] How to add widgets in the every site Link to this post

    Thanks for your reply,

    But the problem is exactly the content of the Sidebar function. How can I know that the Sidebar should be displayed or not? I thought I can check it with a SQL query, but that was the problem.

    The allocation which defines, that there has to be a Sidebar (table Page and Page_Live the entries of ID and SidebarID), is always there even when there shouldn't be a Sidebar. So how can I proof, if there should be a Sidebar or not?

  • FungshuiElephant
    Avatar
    Community Member
    57 Posts

    Re: [Solved] How to add widgets in the every site Link to this post

    The SidebarID column in the Page table should have an ID in it as long as you have a WidgetArea defined in your Page.php file. The ID is of the Area not of the Widgets. So even if you don't have any widgets you still have an empty WidgetArea and it should be displayed.

    If you don't want that behaviour then you'll have to write your own function in Page.php that checks to see if the WidgetArea should be rendered and returns true or false accordingly.

    So write a function called IHaveWidgets in Page.php and then add

    <% if IHaveWidgets %>
    <div id="Widgetbar" class="typography">
    $Sidebar
    </div>
    <% end_if %>


    to your template file.

    The function IHaveWidgets just does an SQL query to check if the page really has widgets...

    function IHaveWidgets () {
    $myPage = DataObject::get_one("SiteTree", "`SiteTree`.ID = $this->ID");
    if (myPage) {
    $widgetArea = $myPage->SidebarID;
    $widgets = DataObject::get("Widget", "ParentID = $widgetArea");
    if ($widgets) {
    return true;
    }
    }
    return false;
    }

    which can be shortened to:

    function IHaveWidgets () {
    return DataObject::get("Widget", "ParentID = $this->SidebarID");
    }

  • funfried
    Avatar
    Community Member
    22 Posts

    Re: [Solved] How to add widgets in the every site Link to this post

    Thank you very much for your help!

    That's excatly what I searched for. I can't test it now, but I'll give it a try as soon as possible and post the results here.

    Thank you very much.

    Kind regards,

    Fabi

  • FungshuiElephant
    Avatar
    Community Member
    57 Posts

    Re: [Solved] How to add widgets in the every site Link to this post

    You're welcome.
    If it works then you might want to change the title to:
    [Solved] Check if site has widget
    so that folks can find the answer quickly. It seems a sensible thing to do - I haven't actually found any forum rules yet. ;-)

  • funfried
    Avatar
    Community Member
    22 Posts

    Re: [Solved] How to add widgets in the every site Link to this post

    Ok, I will do it and I'll post the whole changes of the code (the changes I've already made and your advice), so that it is quiet easy to do it for others

  • funfried
    Avatar
    Community Member
    22 Posts

    Re: [Solved] How to add widgets in the every site Link to this post

    Hi,

    IT WORKED!!! Again, thank you very much, here is what I've changed:

    mysite/code/Page.php (add only this part):

    [...]

    class Page_Controller extends ContentController {

       [...]

       function HasWidgets()
       {
          if($this->SidebarID)
          {
             return DataObject::get("Widget", "ParentID = $this->SidebarID");
          }

          return false;
       }
    }

    --------------------------------------------------------------------------------------------

    themes/[YOUR_THEME_NAME]/templates/Layout/Page.ss (the whole file):

    <div class="typography">
    <% if HasWidgets %>
    <div id="Widgetbar" class="typography">
    $Sidebar
    </div>
    <% end_if %>
    <% if Menu(2) %>
    <% include SideBar %>
    <% end_if %>

    <% if HasWidgets %>
    <div id="Content">
    <% else %>
    <% if Menu(2) %>
       <div id="Content">
    <% end_if %>
    <% end_if %>

    <% if Level(2) %>
    <% include BreadCrumbs %>
    <% end_if %>

    <h2>$Title</h2>

    $Content
    $Form
    $PageComments

    <% if HasWidgets %>
    </div>
    <% else %>
    <% if Menu(2) %>
    </div>
    <% end_if %>
    <% end_if %>
    </div>

    --------------------------------------------------------------------------------------------

    themes/[YOUR_THEME_NAME]/css/layout.css (only make these changes):

    These changes are depending to your theme, I changed it, because the originally Sidebar is on the left in my theme and I wanted to have the widgets on the right, so I copied the complete style information for the Sidebar and changed every css ID from Sidebar into Widgetbar and changed the float from left to right.

    If you want to use the same css layout for the Widgetbar as the Sidebar is, then change the following the the mentioned file (the file you should have changed before):

    themes/[YOUR_THEME_NAME]/templates/Layout/Page.ss (change only this part):

    <div class="typography">
    <% if HasWidgets %>
    <div id="Sidebar" class="typography"> <--- id changed from Widgetbar into Sidebar
    $Sidebar
    </div>

    [...]

    I hope this helps not only me.

    Thanks everybody for their advices!

    If anyone have problems with these changes, please fell free to ask.

    Kind regards,

    Fabi

    (I've changed to topic from "Check if site has widget", because this is not a very good title to find this thread, when you're searching for how to add widgets in the every site.)

  • webcook
    Avatar
    Community Member
    20 Posts

    Re: [Solved] How to add widgets in the every site Link to this post

    hi

    i developed new widget in silver stripe.Its functionality is very simple .just add one text field and text area field.
    I works pefetctly in admin and stored in data base also perfectly.

    This is my widget coding
    class NewStoryWidget extends Widget {
       static $db = array(
          'NewStoryTitle' => 'Varchar(255)',
          'NewStoryContent' => 'Text'
       );

       static $title = 'Title';
       static $cmsTitle = 'Description';
       static $description = 'Adds HTML content to the widget sidebar. <br />(Save and refresh the page if you cannot see the text editor field.)';
       
       function Title() {
          return $this->WidgetTitle ? $this->WidgetTitle : self::$title;
       }
       
       function MyStory() {
        $pages = DataObject::get('NewStoryWidget', '', '', '',2);
          $return = new DataObjectSet();
          foreach($pages as $page) {
             $versioned = $page->NewStoryTitle;
             $author = $page->NewStoryContent;
             $return->push(new ArrayData(array('Title' => $versioned, 'Desc' => $author)));
          }
          return $return ;
       }

       function getCMSFields() {
          $fields = new FieldSet();
          $fields->push(new TextField('NewStoryTitle'));
          $fields->push(new TextareaField('NewStoryContent', 'Story description'));
          return $fields;
       }}

    //this is my NewStoryWidget.ss
    <div class="NewStoryWidget">
    <% if $MyStory %>
       <% control $MyStory %>
        $Story
        $Desc
       <% end_control %>
    <% end_if %>
    </div>

    Suppose if add the
    $SideBar in below of the SideBar.ss

    It showing error like this
    Parse error: syntax error, unexpected '}' in C:\WINDOWS\Temp\silverstripe-cacheD--xampp-htdocs-srganesan-Projects-mywid\.cacheD..xampp.htdocs.srganesan.Projects.mywid.themes.blackcandy.templates.Layout.Page.ss on line 209

    Please give me some advice and show ,my widgets values in the pages.

    Am waiting for your reply
    Thanks

  • FungshuiElephant
    Avatar
    Community Member
    57 Posts

    Re: [Solved] How to add widgets in the every site Link to this post

    Hi there,

    sounds like you have an extra curly bracket in Page.ss. If you post that we might be able to help further.

    3403 Views
Page: 1 2
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.