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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Choosing a URL from sitetree


Reply


30 Posts   6925 Views

Avatar
steve_nyhof

Community Member, 224 Posts

25 February 2010 at 2:25am

Thank you banal for your help here. I have this system about to roll out and I decided to add the dropdowns at the last moment just to make it more straight forward. I have a webinar tonight so I am hoping to get this working.

I changed the fields to Varchar(255) to make sure I cover someones longtail page name. This did not work.

'DivertURL' => 'Varchar(255)',
'DivertURLTwo' => 'Varchar(255)',

I do not have two pages, this is setup in Page.php - see screen shot.

I am following what you are saying, but programming is not second nature to me.

I have this on the page already...

public static $has_one = array(
"HeaderImage" => "Image",
    "BackgroundImage" => "Image",
    "BulletImage" => "Image",
    "BodyImage" => "Image",
    "FooterImage" => "Image",
    "PopupImage" => "Image",
    "PeelSmallImage" => "Image",
    "PeelLargeImage" => "Image",
   
);

Can you please provide some code as to what you are proposing?
Thank you,
Steve

Attached Files
Avatar
banal

Community Member, 901 Posts

25 February 2010 at 3:25am

Hi Steve

Ok, since Varchar didn't improve things, I suggest you get and store the URLSegments yourself (upon saving). Let's walk through some steps:
1) In the $has_one array add:

'DivertPage' => 'SiteTree',
'DivertPageTwo' => 'SiteTree'

2) Don't forget dev/build

3) In your TreeDropdown use ID instead of URLSegment and set the FieldName to DivertPageID and DivertPageTwoID... much like described in the other posts in this thread.

4) Then, to populate your Varchar fields, add the following method to your Page class:

public function onBeforeWrite(){
   if($this->DivertPageID){
      $this->DivertURL = $this->DivertPage()->URLSegment;
   } else {
      $this->DivertURL = '';
   }
   
   if($this->DivertPageTwoID){
      $this->DivertURLTwo = $this->DivertPageTwo()->URLSegment;
   } else {
      $this->DivertURLTwo = '';
   }
}

This will check if a Page-Relation is set, and update the DivertURL Fields accordingly.

Avatar
steve_nyhof

Community Member, 224 Posts

25 February 2010 at 4:14am

Edited: 25/02/2010 7:53am

Hi banal,

EDIT: I did not read your note correctly - give me a few minutes to fix things...
Still not working

Here is what I did and what the results where...

public static $has_one = array(
"HeaderImage" => "Image",
    "BackgroundImage" => "Image",
    "BulletImage" => "Image",
    "BodyImage" => "Image",
    "FooterImage" => "Image",
    "PopupImage" => "Image",
    "PeelSmallImage" => "Image",
    "PeelLargeImage" => "Image",
    'DivertPage' => 'SiteTree',
    'DivertPageTwo' => 'SiteTree'
   
);

$fields->addFieldToTab("Root.Content.Stratagem", new TreeDropdownField('DivertPageID', 'Select Offer Ended Page:', 'SiteTree', 'ID', 'MenuTitle'));

$fields->addFieldToTab("Root.Content.Stratagem", new TreeDropdownField('DivertPageTwoID', 'Select Offer Ended Page:', 'SiteTree', 'ID', 'MenuTitle'));

And in class...

public function onBeforeWrite(){
if($this->DivertPageID){
$this->DivertURL = $this->DivertPage()->URLSegment;
} else {
$this->DivertURL = '';
}

if($this->DivertPageTwoID){
$this->DivertURLTwo = $this->DivertPageTwo()->URLSegment;
} else {
$this->DivertURLTwo = '';
}
}

What is calling onBeforeWrite ?

EDIT:
That seems to be doing something, but now it looks the same - the ..Two field is showing up in the dropdown like before, but the first dropdown (DivertPageID) is still blank. The table is updated fine, just not populating the dropdown form.

Steve

Avatar
steve_nyhof

Community Member, 224 Posts

25 February 2010 at 4:31am

In your function you have the name "DivertPage" and "DivertPageTwo" - are these supposed to be fields?

Avatar
banal

Community Member, 901 Posts

25 February 2010 at 6:21am

Hi Steve

Yes, "DivertPage" and "DivertPageTwo" are relation fields (defined in the $has_one array).
Your code looks fine to me. I just tested this with a local SilverStripe 2.3.5 and 2.3.4 install and all the previously discussed approaches actually work... even the one where you set the URLSegment directly (http://silverstripe.org/general-questions/show/262710?start=8#post279641).

So my guess is you either didn't run dev/build or you got some JS error that prevents the dropdown from selecting the correct value. Do you get JavaScript errors by any chance?

Here's the code that works for me: http://pastie.org/840793

Avatar
steve_nyhof

Community Member, 224 Posts

25 February 2010 at 7:52am

Edited: 25/02/2010 8:07am

I appreciate your help, I will need to do some back tracking to see if I have something wrong with something else.

I kept your code on the page so I can refer back to it.

Just so you know, I did remove the working dropdown just to see if having two was a problem, still the field came up blank.

It's just right there, when the page loads, and I just touch the dropdown arrow, the value fills in - bam! Anyway...

EDIT: I removed all my fields except the two, and it still was not working. I may try a completely clean Page.php and slowly add the fields in...

EDIT: I removed everything until all I had was what you code shows, and it still gives me the same thing! I don't get it.

Thank you again,
Steve

Avatar
banal

Community Member, 901 Posts

25 February 2010 at 8:50am

That's really strange.
What version of SilverStripe are you using?
If you're running Firefox, install Firebug (http://getfirebug.com) and check the JavaScript console for any errors.

Avatar
steve_nyhof

Community Member, 224 Posts

25 February 2010 at 9:28am

I am using 2.3.3

I always Dev/Build

I use FF exclusively, but text my results on IE and Chrome.

Ever sense I updated to Windows 7, I have been unable to install FB and I miss it greatly! Answered so many questions. Not sure what to do. Don't have time to try and figure it out either - trying to get a new business off the ground.

Steve