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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Upgrading SilverStripe /

Ask questions about upgrading SilverStripe to the latest version.

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

Converting a URLSegment to a Full-Path Link


Go to End


3 Posts   2878 Views

Avatar
Garrett

Community Member, 245 Posts

10 December 2010 at 6:10am

Hi,

I just upgraded to 2.4.3 from 2.3.* and, as we know, with the advent of hierarchical URLs, URLSegment can no longer be used as a link since the nesting is not stored in that field. I realize that this was considered bad practice in the first place, but perhaps had I known how to convert a URLSegment TO a Link() in the first place then I wouldn't have done it ;)

That said, let's get to it. I use a lot of custom queries on my site using DB::query() and SQLQuery(). In these queries, the Link() function is meaningless. So I am getting the URLSegment from the SiteTree table and then just using that value as the Link. Now, as stated above, this doesn't work, because these pages are now nested within other parent pages.

I need to know how I can "calculate" the FULL URL from this URLSegment, much like the redirects performed by the MigrateSiteTreeLinkingTask action does. There has to be a method for this, but I haven't found it yet. Any help out there?

Thanks in advance,
Garrett

Avatar
Willr

Forum Moderator, 5523 Posts

10 December 2010 at 12:09pm

Well like you said you should be using $Link in templates etc. It looks like SiteTree's RelativeLink() function handles making the full link. You will still need to get the actual object record to call that though. AFAIK there doesn't appear to be a static function to 'convert' foo/ to /about-us/foo without orignally having the foo page object (i.e dataobject::get()) to start with.

The MigrateSiteTreeLinking doesn't actually rewrite the URL, it saves the ID then on output it renders the Link() function of the page.

Avatar
Garrett

Community Member, 245 Posts

11 December 2010 at 5:39am

Thanks for your reply @willr. How do you recommend getting the object record from the URLSegment?? Or, the ID. I can get the ID in my query, obviously. I guess you're saying use DataObject::get_by_id() and then pass that result to RelativeLink()?

//Garrett