Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » SiteTree selection not saved

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

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

Page: 1
Go to End
Author Topic: 2546 Views
  • joelg
    Avatar
    Community Member
    129 Posts

    SiteTree selection not saved Link to this post

    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

  • ajshort
    Avatar
    Community Member
    244 Posts

    Re: SiteTree selection not saved Link to this post

    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.

  • joelg
    Avatar
    Community Member
    129 Posts

    Re: SiteTree selection not saved Link to this post

    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

  • RS26
    Avatar
    Community Member
    11 Posts

    Re: SiteTree selection not saved Link to this post

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

  • simon_w
    Avatar
    Forum Moderator
    471 Posts

    Re: SiteTree selection not saved Link to this post

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

    new TreeDropdownField("PhotoLinkID"...

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