Jump to:

1160 Posts in 2632 Topics by 409 members

Upgrading SilverStripe

SilverStripe Forums » Upgrading SilverStripe » Converting a URLSegment to a Full-Path Link

Ask questions about upgrading SilverStripe to the latest version.

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

Page: 1
Go to End
Author Topic: 1835 Views
  • Garrett
    Avatar
    Community Member
    245 Posts

    Converting a URLSegment to a Full-Path Link Link to this post

    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

  • Willr
    Avatar
    Forum Moderator
    5508 Posts

    Re: Converting a URLSegment to a Full-Path Link Link to this post

    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.

  • Garrett
    Avatar
    Community Member
    245 Posts

    Re: Converting a URLSegment to a Full-Path Link Link to this post

    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

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