Jump to:

794 Posts in 408 Topics by 304 members

Widgets

SilverStripe Forums » Widgets » Save widget content on every page - nearly solved?

Discuss SilverStripe Widgets.

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

Page: 1
Go to End
Author Topic: 638 Views
  • bones
    Avatar
    Community Member
    104 Posts

    Save widget content on every page - nearly solved? Link to this post

    I want to add the same widgets to every page on my website, so adding/editing widgets on my HomePage updates every page.

    I've followed the tutorial, and am SO close to getting this to work. I just need a little help on the very last stage.

    In mysite/code/HomePage.php, I've added:

       static $has_one = array(
          "MyWidgets" => "WidgetArea",
       );

    and

    function getCMSFields() {
       $fields = parent::getCMSFields();
          $fields->addFieldToTab("Root.Content.Widgets", new WidgetAreaEditor("MyWidgets"));
       return $fields;
    }

    and to templatename/templates/Layout/HomePage.ss I added this:

    $MyWidgets

    So far so good. This works great.

    However, now I need to get this data onto all other pages. $MyWidgets doesn't work on other pages without a little help, and that's where I'm getting stuck.
    I've tried the following on mysite/code/Page.php, but it doesn't work.

    class Page_Controller extends ContentController {
       public function getMyWidgets(){
          $obj = DataObject::get_one('HomePage');
       return $obj->MyWidgets;
       }

    I would be very grateful if someone could help me with this last part, please. Thank you.

  • martimiz
    Avatar
    Forum Moderator
    1067 Posts

    Re: Save widget content on every page - nearly solved? Link to this post

    A really simple way to do this, that seems to work - but I haven't tested it any further, so that'll be up to you

    Suppose you want to reproduce your homepage widgets on every other page. Then in your Page_Controller:

       function getWidgetPage() {
          return Dataobject::get('SiteTree', "URLSegment = 'home'");
       }

    And in your template:

    <% control getWidgetPage %>$MyWidgets<% end_control %>

  • bones
    Avatar
    Community Member
    104 Posts

    Re: Save widget content on every page - nearly solved? Link to this post

    Perfect! Thank you.

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