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

2 April 2009 at 10:45pm

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?

Avatar
FungshuiElephant

Community Member, 57 Posts

3 April 2009 at 2:19am

Edited: 03/04/2009 2:20am

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");
}

Avatar
funfried

Community Member, 22 Posts

3 April 2009 at 2:33am

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

Avatar
FungshuiElephant

Community Member, 57 Posts

3 April 2009 at 3:12am

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

Avatar
funfried

Community Member, 22 Posts

3 April 2009 at 3:22am

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

Avatar
funfried

Community Member, 22 Posts

3 April 2009 at 7:07am

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

Avatar
webcook

Community Member, 20 Posts

1 June 2009 at 7:55pm

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

Avatar
FungshuiElephant

Community Member, 57 Posts

3 June 2009 at 11:21am

Hi there,

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

Go to Top