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.

Archive

Our old forums are still available as a read-only archive.

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

SiteTree selection not saved


Reply

5 Posts   2569 Views

Avatar
joelg

3 September 2008 at 8:53am Community Member, 129 Posts

Hi Everyone

I'm a newbie on Silverstripe and have a question. I created a SiteTree field in silverstripe and it shows up in my tab. However, when I choose a page from the sitetree and click save, it doesn't save anything. Here's the code in Page.php:

static $has_one = array(
'Photo' => 'Image',
'PhotoLink' => 'SiteTree',
'Photo2' => 'Image',
'PhotoLink2' => 'SiteTree',
'Photo3' => 'Image',
'PhotoLink3' => 'SiteTree'
);
   
function getCMSFields() {
$fields = parent::getCMSFields();
   
$treedropdownfield = new TreeDropdownField("PhotoLink", "Choose a page that wish to link to:", "SiteTree");
    $treedropdownfield2 = new TreeDropdownField("PhotoLink2", "Choose a page that wish to link to:", "SiteTree");
    $treedropdownfield3 = new TreeDropdownField("PhotoLink3", "Choose a page that wish to link to:", "SiteTree");
   
$fields->addFieldToTab("Root.Content.RightImages", new ImageField('Photo'));
$fields->addFieldToTab("Root.Content.RightImages", new TextField('PhotoText'));
$fields->addFieldToTab("Root.Content.RightImages", $treedropdownfield);
$fields->addFieldToTab("Root.Content.RightImages", new ImageField('Photo2'));
$fields->addFieldToTab("Root.Content.RightImages", new TextField('PhotoText2'));
$fields->addFieldToTab("Root.Content.RightImages", $treedropdownfield2);
$fields->addFieldToTab("Root.Content.RightImages", new ImageField('Photo3'));
$fields->addFieldToTab("Root.Content.RightImages", new TextField('PhotoText3'));
    $fields->addFieldToTab("Root.Content.RightImages", $treedropdownfield3);

return $fields;
}

As you can it's pretty simply, but why doesn't Silverstripe save the selected page?

And a second question when I succed in saving an internal page, how do I acces the site in my template Page.ss

Many thanx

Joel

Avatar
ajshort

3 September 2008 at 10:02am Community Member, 244 Posts

Hi joelg,

when you visit the page in the CMS, do all the fields appear? If so, its probably that you ahvent rebuilt the database. To do this, visit yoursite.com/db/build?flush - this will create all the new DB fields automatically.

If youve already done this, you need to make sure that your adding this code to the Page, not the Page_Controller class.

Avatar
joelg

3 September 2008 at 6:23pm (Last edited: 3 September 2008 6:23pm), Community Member, 129 Posts

Hi Ajshort

I did rebuild the database and that wasn't the issue, however I'v worked out a solution that works for know, but i'm pretty sure it's not best practice. I created three Integers, and that makes it possible to save the siteTree selection. Here's mys Page.php code:

class Page extends SiteTree {
   static $db = array(
      'PhotoText' => 'Text',
      'PhotoText2' => 'Text',
      'PhotoText3' => 'Text',
      'PhotoLink' => 'Int',
      'PhotoLink2' => 'Int',
      'PhotoLink3' => 'Int'
   );
   
   static $defaults = array(
   );

   static $has_one = array(
'Photo' => 'Image',
'PhotoLink' => 'Page',
'Photo2' => 'Image',
'PhotoLink2' => 'Page',
'Photo3' => 'Image',
'PhotoLink3' => 'Page'
);
   
function getCMSFields() {
$fields = parent::getCMSFields();
      
$fields->addFieldToTab("Root.Content.RightImages", new ImageField('Photo'));
$fields->addFieldToTab("Root.Content.RightImages", new TextField('PhotoText'));
$fields->addFieldToTab("Root.Content.RightImages", new TreeDropdownField("PhotoLink", "Choose a page that wish to link to:", "SiteTree"));
$fields->addFieldToTab("Root.Content.RightImages", new ImageField('Photo2'));
$fields->addFieldToTab("Root.Content.RightImages", new TextField('PhotoText2'));
$fields->addFieldToTab("Root.Content.RightImages", new TreeDropdownField("PhotoLink2", "Choose a page that wish to link to:", "SiteTree"));
$fields->addFieldToTab("Root.Content.RightImages", new ImageField('Photo3'));
$fields->addFieldToTab("Root.Content.RightImages", new TextField('PhotoText3'));
    $fields->addFieldToTab("Root.Content.RightImages", new TreeDropdownField("PhotoLink3", "Choose a page that wish to link to:", "SiteTree"));

return $fields;
}

   function getUrl() {
      return DataObject::get_by_id("Page",$this->PhotoLink);
   }
   function getUrl2() {
      return DataObject::get_by_id("Page",$this->PhotoLink2);
   }
   function getUrl3() {
      return DataObject::get_by_id("Page",$this->PhotoLink3);
   }
}

And here's a little part of my template section...

<div id="foto"><a href="<% control getUrl %> $Link <% end_control %>">$Photo.SetWidth(230)</a>
                   <div id="fotoText"><a href="<% control getUrl %> $Link <% end_control %>">$PhotoText</a></div></div>

As you can see, I'm trying to create some sort of banner images, which users can click and be redirected to another page on the websites. This solution works, but it could be better.

Thanx for you help:-) And please let me know if there's an easier solution...

Joel

Avatar
RS26

5 December 2008 at 6:47am (Last edited: 5 December 2008 6:47am), Community Member, 11 Posts

Hey Joelg... I'm looking for the same solution. Did you have any issues with your method after you tried it ?

Avatar
simon_w

5 December 2008 at 8:58am Forum Moderator, 474 Posts

Instead of using integers, keep the SiteTree types, but use the ID field in the TreeDropDownField, so

new TreeDropdownField("PhotoLinkID"...