Jump to:

791 Posts in 369 Topics by 303 members

Widgets

SilverStripe Forums » Widgets » Inherit Widgets

Discuss SilverStripe Widgets.

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

Page: 1 2
Go to End
Author Topic: 3248 Views
  • tzeller
    Avatar
    Community Member
    26 Posts

    Re: Inherit Widgets Link to this post

    thanks dio5, as i am not so deep in silverstripe yet do you have any idea how and where i should add the method.

    mille gracie, i really need help here.

  • Nivanka
    Avatar
    Community Member
    395 Posts

    Re: Inherit Widgets Link to this post

    are you planning to use the same sidebar for every page?

    then what you can do is to define a new age type, something like this.

    <?php

    class SidebarPage extends Page{

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

    }

    class SidebarPage_Controller extends Page_Controller{

    }

    ?>

    then add this method to the Page_Controller class in the mysite/code/Page.php

    <?php

    class Page_Controller{

    function getSidebar(){
    $page = DataObject::get_one("SidebarPage");

    return ($page) ? $page->Sibebar : false;

    }

    }

    ?>

    then on your template files add the following to retrieve your sidebar.

    $getSidebar

    This will fix your issue

  • tzeller
    Avatar
    Community Member
    26 Posts

    Re: Inherit Widgets Link to this post

    i am very sorry - i think i am not making my point clear.

    i want to be able to create a new page and add some widgets to the page.
    but when i am adding a children pages in the tree i have to add manually again the widgets.
    it doesn't automatically takes the widgets from the parent page.

    i have extended all pages with the widget by using this code in page.php:
    ---

    <?php

    class Page extends SiteTree {
       
       public static $db = array(
       );
       
       public static $has_one = array(
             "Sidebar" => "WidgetArea",
             'PageImage' => 'Image'
       );

       function getCMSFields() {
       $fields = parent::getCMSFields();
       $fields->addFieldToTab("Root.Content.Widgets", new WidgetAreaEditor("Sidebar"));
    $fields->addFieldToTab("Root.Content.Images", new ImageField('PageImage', 'Page Image'));
       return $fields;
    }
       
    }

    class Page_Controller extends ContentController {   
       
       public function init() {
          parent::init();
          
          /*set locale */
    $curLang=Session::get('currentLang');
    switch($curLang) {
    case "en":
    i18n::set_locale('en_US');;
    break;
    case "de":
    i18n::set_locale('de_DE');
    break;
    }
    /*end of set locale*/

          Requirements::themedCSS("layout");
          Requirements::themedCSS("typography");
          Requirements::themedCSS("form");
       }

       function SearchForm() {
    $searchText = isset($this->Query) ? $this->Query : 'Search';
          
    $fields = new FieldSet(
    new TextField("Search", "", $searchText)
    );

    $actions = new FieldSet(
    new FormAction('results', 'Go')
    );

    return new SearchForm($this, "SearchForm", $fields, $actions);
    }

    function results($data, $form){
    $data = array(
    'Results' => $form->getResults(),
    'Query' => $form->getSearchQuery(),
    'Title' => 'Search Results'
    );
       
    return $this->customise($data)->renderWith(array('Page_results', 'Page'));
    }

    }

    ?>

    ---

    i have also changed the product.php (i.e.) so a product can have a product as parent. At the moment i am having a sitetree like this

    ..
    ProductGroup
    -Product A
    --Product AA
    -Product B
    --Product BB

    I have changed the product.php because i want to be able to have a product packages which include single products as well as single products as stand alone:

    ---

    static $default_parent = 'Product';

    ---

    So is there a function which i could use that all the children pages display the widgets of the parent-page in the cms as well as on the front end?

    thanks so much... whenever you are in germany i will invite you to an "Sauerkraut-Dinner" ;-)

  • Nivanka
    Avatar
    Community Member
    395 Posts

    Re: Inherit Widgets Link to this post

    okay in that case it can be done like this.

    create a new page type

    <?php

    class ParentPage extends Page{

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

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

    }

    class ParentPage_Controller extends Page_Controller{

    }

    ?>

    then add this code to the Page.php

    <?php

    class Page_Controller{

    function getSidebar(){
    $page = DataObject::get_one("ParentPage","ID =" . $this->ID );

    return ($page) ? $page->Sibebar : false;

    }

    }

    ?>

    then you can use it I think

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