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.

Widgets /

Discuss SilverStripe Widgets.

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

Blog Widget duplicated in Sidebar - Please Help!


Go to End
Reply


14 Posts   2824 Views

Avatar
arthurdent

Community Member, 184 Posts

8 March 2011 at 2:29pm

Hopefully somebody can help me out here. I have a blog page which is currently showing two Widget panels in the sidebar. One is the default setting, the second is due to a modification I made to the page code to ensure that the widget panel appeared beneath the sidebar navigation panel in my template (as opposed to on top of my navigation template)...

So now, having generated a second Widget panel I set about disabling the first. I found a useful tutorial under [url]http://doc.silverstripe.org/sapphire/en/topics/widgets[/url] which told me how to do this as follows:

--

But what if I have widgets on my blog currently??

If you currently have a blog installed, the widget fields are going to double up on those pages (as the blog extends the Page class). One way to fix this is to comment out line 30 in BlogHolder.php and remove the DB entry by running a /db/build.

** blog/code/BlogHolder.php **
   
<?php

class BlogHolder extends Page {

........
static $has_one = array(
// "SideBar" => "WidgetArea", COMMENT OUT
'Newsletter' => 'NewsletterType'
.......
function getCMSFields() {
$fields = parent::getCMSFields();
$fields->removeFieldFromTab("Root.Content.Main","Content");
// $fields->addFieldToTab("Root.Content.Widgets", new WidgetAreaEditor("SideBar")); COMMENT OUT

........

Then you can use the Widget area you defined on Page.php

--

Yeah, right... First problem is the build of SilverStripe that I am using is obviously a later version. In the file BlogHolder.php does not extend the Page class, it extends the Blog tree. The code is completely different. And commenting out any mention of Sidebar in this file has no apparent effect on the page, even after running db/build

Am I working on the wrong file? Is there a more up-to-date method? Can anybody give me any pointers?

T.I.A...

Avatar
Willr

Forum Moderator, 5513 Posts

8 March 2011 at 5:03pm

Hi,

Thanks for pointing out the issue with the documentation it needs to be updated. If you notice any old information like that please report it as a ticket (http://open.silverstripe.org/newticket) with the component doc.silverstripe.com Content.

If it is not in BlogHolder you want to look at the BlogTree.php code and comment out the SideBar in there. A better way of doing it now is to actually use a decorator (more information on the doc site). Decorate BlogTree and use updateCMSFields() to remove the WidgetArea.

<?php

class MyBlogTree extends DataObjectDecorator {

function updateCMSFields(&$fields) {
$fields->removeFieldByName('SideBar');
}
}

Avatar
arthurdent

Community Member, 184 Posts

9 March 2011 at 10:37am

Willr, thanks once again for your help and advice. I'm looking at using a 'decorator' to achieve this. I've looked at the doc site but I'm not 100% sure what I should be doing (while I know HTML, CSS and a smattering of PHP, I'm really a designer, not a programmer...)

Should the code you supplied below be added to the BlogTree.php file? Is this what is meant by 'decorating'?!

Just moving a little bit outside my comfort zone here...

Avatar
socks

Community Member, 191 Posts

6 April 2011 at 6:36pm

I'm finding that even commenting out those lines or using a decorator in BlogTree.php isn't enough. When doing so, I get an "Error Saving Content" when trying to publish a BlogEntry or BlogHolder page.

The only thing I can think of is to rename the WidgetArea on my Page.php (ie "SideBarWidgets" => "WidgetArea"). I'd rather not do that since I'll have to go back to all of my pages and re-add the widgets.

Any other suggestions on the best way to have Widgets and Blog Widgets?

Avatar
Mackodlak

Community Member, 95 Posts

18 April 2011 at 9:59pm

Hello guys,

i see you all have this kind of problems. I myself have found an elegant solution to this problem. Most of us don't need widgets on absolutely every page we use, so if you need them on homepage,you will likely make another page controller homepage.php that extends page.php so add widget areas you wanna use there instead on page controller so that homepage, and homepage alone has access to widgets.
also, if you then need them on some of the other pages, make separate controllers for them also.
This is sth i think is best, cause on hompage i use 3 or 4 separate widget areas, on blog you use only 1 (but ofc, if you need them you can always add them on blob and blogholder controllers where you need them), i also have 2 more pages that use some widgets, but in 1 and 2 areas - so it's nice to have a separate page controller for all of them anyways - so why place it all on page.php?

Mby i'm doing sth "wrong" or the way it is not supposed to be done, but it works and I think it's an elegant way of using widgets and some other stuff.

Hope you understand what i've said and that i'tll help some of you!

Avatar
MarijnKampf

Community Member, 164 Posts

7 January 2012 at 2:27am

Found this toppic whilst searching for solution to the same problem.

Willr's solution was 90% there. My modification without needing to edit core files ended up as:

Create mysite/code/NoWidgetsBlogTree.php

<?php

class NoWidgetsBlogTree extends DataObjectDecorator {
   function updateCMSFields(&$fields) {
      $fields->removeByName('SideBar');
   }
}


in mysite/_config.php add:

Object::add_extension('BlogTree', 'NoWidgetsBlogTree');

Avatar
greenpea

Community Member, 19 Posts

20 June 2012 at 11:56pm

Hi all
MarijnKampf I used your code and it has removed duplicates from the blog page. Sadly now I get am unable to add any widgets at all to the blog page. I select a widget, add it and when I save the widget disappears.
All other page types are saving widgets fine.
(I'm running v 2.4.5)
Any tips on what I might have missed?

Avatar
MarijnKampf

Community Member, 164 Posts

21 June 2012 at 1:15am

Not quite sure why it would do that. Do the Widgets work on pages that are not a blog?

Go to Top