Jump to:

3375 Posts in 999 Topics by 712 members

Data Model Questions

SilverStripe Forums » Data Model Questions » [SOLVED] URL slug instead of ID on DataObject?

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

Page: 1 2
Go to End
Author Topic: 3895 Views
  • LesC
    Avatar
    Community Member
    70 Posts

    [SOLVED] URL slug instead of ID on DataObject? Link to this post

    Hi,

    I've created a news section for my site that uses the awesome DataObjectManager, and with some help from UncleCheese I've got it working with categories, with the end goal of having a much easier to use news archive in the CMS (there's likely to be more than a few hundred news articles in the next year or so, and the sitetree system can't handle it).

    I've got each NewsArticle displaying on its own 'page', by using a function on the NewsHolder (which sits at /news in the site):

    function display() {
    $params = Director::urlParams();
    $id = (int)$params['ID'];
    $object = DataObject::get_by_id("NewsArticle", $id);
    if($object) {
    return $this
    ->customise(array('NewsArticle' => $object))
    ->renderWith(array('NewsArticle', 'Page'));
    } else {
    Director::redirect('not-found');
    }
    }

    So when I go to /news/display/1 it shows me the first NewsArticle in a nice template.

    My question is this - and I know it's kind of an overlap with how Objects and Pages work, but anyway - is there a way to generate a friendly URL instead of displaying by ID? I'd like to get something like /news/display/news-title-here working, but can't work out the best way to do it.

    * Should I create a url-slug field in the NewsArticleObject, and set that on creation somehow, then call that from the Display function, or,

    * Is there a function for returning a friendly url based on a field on the Object (e.g. Title)?

    Any help on this is much appreciated!

    Les

  • Pigeon
    Avatar
    Community Member
    243 Posts

    Re: [SOLVED] URL slug instead of ID on DataObject? Link to this post

    Hi LesC,

    Surely getting the dataobject by title and passing that in the URL is how you would not use the ID?

    So don't use get_by_id, use get_one() and have field that can be 'url' and then search for the data object with the URL. Simple.

    something like:

    $record = DataObject::get_one('NewsArticle', "`URL` = '".$id."'");

    I think that should work

    Worth a shot i think.

  • LesC
    Avatar
    Community Member
    70 Posts

    Re: [SOLVED] URL slug instead of ID on DataObject? Link to this post

    Cheers Pigeon,

    Yup I'm going to try what you put, but do you know if SS has the functionality to create the URL slug and save it automatically? I'm guessing that the way it's handled when you create a page in the CMS that javascript is what generates the url title but can't seem to confirm that.

    Or, in Rails models you can run a function 'before save' so that you can do stuff before data inputs are saved - do the models in SS cope with 'do this before saving'?

    I've found a nifty function for converting the title into a URL slug:

    function slug($str)
    {
       $str = strtolower(trim($str));
       $str = preg_replace('/[^a-z0-9-]/', '-', $str);
       $str = preg_replace('/-+/', "-", $str);
       return $str;
    }

    So hopefully there's a way for me to use this if SS doesn't have in built functionality.

    Anyone have any ideas?

  • Pigeon
    Avatar
    Community Member
    243 Posts

    Re: [SOLVED] URL slug instead of ID on DataObject? Link to this post

    There is a 'onBeforeWrite' method that you can use in the page class.

    something like:

    function onBeforeWrite() {
    ...
    parent::onBeforeWrite();
    }

    See an example of use: http://www.silverstripe.org/archive/show/7431?start=0#post7656

    Hope that is of some use.

  • LesC
    Avatar
    Community Member
    70 Posts

    Re: [SOLVED] URL slug instead of ID on DataObject? Link to this post

    Thanks for that Pigeon, I'll take a look and post my findings when I can

  • Martijn
    Avatar
    Community Member
    271 Posts

    Re: [SOLVED] URL slug instead of ID on DataObject? Link to this post

    Why not use this:

       function onBeforeWrite () {
          parent::onBeforeWrite ();
          if($this->Title)
             $this->URLSegment = SiteTree::generateURLSegment($this->Title);
       }

  • LesC
    Avatar
    Community Member
    70 Posts

    Re: [SOLVED] URL slug instead of ID on DataObject? Link to this post

    Now that looks interesting!

    I'm hoping to spend sometime on this next week - so thank you for your suggestion Martijn!

  • potion_maker
    Avatar
    Community Member
    36 Posts

    Re: [SOLVED] URL slug instead of ID on DataObject? Link to this post

    Hey Les,
    Did you ever figure out a good way to do this? If so could you explain? Right now my URL looks like site/Employee/FirstName/LastName. It would be nice if it said site/Employee/FirstName LastName

    3895 Views
Page: 1 2
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.