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

Inherit Widgets


Go to End
Reply


12 Posts   3610 Views

Avatar
tzeller

Community Member, 26 Posts

2 February 2009 at 6:39am

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.

Avatar
Nivanka

Community Member, 400 Posts

2 February 2009 at 3:11pm

Edited: 02/02/2009 3:11pm

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

Avatar
tzeller

Community Member, 26 Posts

3 February 2009 at 2:40am

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

Avatar
Nivanka

Community Member, 400 Posts

3 February 2009 at 2:51am

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

Go to Top