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.

Widgets /

Discuss SilverStripe Widgets.

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

[Solved] How to add widgets in the every site


Go to End


16 Posts   6086 Views

Avatar
funfried

Community Member, 22 Posts

1 March 2009 at 2:13am

Edited: 03/04/2009 7:07am

Hi,

is there a possibility to check if a site has widgets?

Because the way I included the widgets is the following:

themes/THEME-NAME/templates/Layout/Page.ss:

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

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

<h2>$Title</h2>

$Content
$Form
$PageComments
</div>
</div>

In this way you will always see the widgets, if there was one added to the site, but the css of the Widgetbar keeps always the size for it, also if there isn't a widget added to a page, therefor I wanted to know, if there is a function to check if a site has a widget, or where to put such a function. The new Page.ss should look something like this:

<div class="typography">

<% if SiteHasWidget() %>
<div id="Widgetbar" class="typography">
$Sidebar
</div>
<% else_if Menu(2) %> // Don't even know how "else if" should look like in ss, please correct me if this is wrong
<% include SideBar %>
<% end_if %>
<div id="Content">

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

<h2>$Title</h2>

$Content
$Form
$PageComments

<% if SiteHasWidget() || Menu(2) %> // I also don't know how to write an "or" operation in ss, please also correct me if I'm wrong
</div>
<% end_if %>
</div>

The CSS for Widgetbar is the same as for Sidebar, the only difference is that, the Widgetbar will be displayed on the right and the original Sidebar will be displayed on the left of the content.

Thanks for any advice.

Regards

Fabian

Avatar
Invader_Zim

Community Member, 141 Posts

1 March 2009 at 4:55am

Hi!

I can't try it out on the machine i am currently sitting, but i think a simple

 
<% if Sidebar %>
    <div id="Widgetbar" class="typography">
         $Sidebar
      </div>
 <% else %>
      <% include SideBar %>
 <% end_if %>

should do it.

Cheers

Avatar
funfried

Community Member, 22 Posts

1 March 2009 at 6:02am

Hi,

thanks for your answer, that worked very well, but only on this sites, where never ever a widget was active, as far as you've put a widget in a site, save it and delete it again, it doesn't work any more.

I've modified the script a bit, it looks now like this:

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

<% if Sidebar %>
<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 Sidebar %>
</div>
<% else %>
<% if Menu(2) %>
</div>
<% end_if %>
<% end_if %>
</div>

Maybe there is a problem in ss when removing widgets from a site.

Avatar
funfried

Community Member, 22 Posts

1 March 2009 at 10:58pm

I've found the bug or the problem, why this is not working, in the table Page and Page_Live the allocation to the widgets (ID -> SidebarID) will not be deleted when removing a widget from a page.

Can anyone tell me where the function in ss is, which removes the widgets from a page?

Avatar
Sam

Administrator, 690 Posts

2 March 2009 at 10:32am

It will be in WidgetAreaEditor::saveInto()

Avatar
funfried

Community Member, 22 Posts

3 March 2009 at 5:13am

Thanks for your answer.

I've found this class, but I don't know the sapphire framework.

Does anybody know where and how the ID->SidebarID allocation in the tables Page and Page_Live will be set and maybe how to remove it when a widget will be removed?

In my opinion this could be the way how widgets could be available for all pages by default in ss! (If any ss developer reads this, please give me a response, what you are thinking of this or maybe you have already a better solution).

Avatar
funfried

Community Member, 22 Posts

9 March 2009 at 3:28am

Sorry for bringing this up again, but does nobody has a solution for this?

Everytime you save a page ss puts an allocation between ID and SidebarID in the table "Page" and if you'll publish it there will be the same allocation in the table "Page_Live", no matter if you've putted a widget on the site or not.

Is this necessary for anything?

I thought these allocations should been deleted, when removing a widget or, shouldn't been created, if there is no widget placed on the page.

Avatar
FungshuiElephant

Community Member, 57 Posts

2 April 2009 at 10:29pm

I'm guessing that you have something like

static $has_one = array (
 "Sidebar" => "WidgetArea"
);

in your Page.php file

Then if you add and delete a widget you get a SidebarID column and widget area id in your Page table and consequently the call

<% if Sidebar %> 

returns true and the stuff in the if block is rendered.

If you add a Sidebar function to the Page.php file

function Sidebar () {
  return false;
}

then

<% if Sidebar %> 

should return false and the stuff in the if block won't get rendered anymore.

Obviously this isn't a complete solution, but if you now add some logic to the Sidebar function to mimic the behaviour of Menu in ContentController.php then it may be possible to do what you want. Hope that gets you a little further.

Go to Top