Jump to:

3430 Posts in 1057 Topics by 734 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Display a DataObject on a new "own" page

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

Page: 1 2 3
Go to End
Author Topic: 8266 Views
  • markus85
    Avatar
    Community Member
    12 Posts

    Display a DataObject on a new "own" page Link to this post

    Hello everybody, I´m very new to silverstripe...

    I want to show a DataObject in an own page how can I do this?

    I created a holder-class wich contains and lists all DataObjects in the database-table - that works fine.
    like here :

    • - Object-String1
    • - Object-String2
    • - Object-String3

    Now I want a link on this "Object-String". A link to a new page wich displays the detailed data of my DataObject.
    The DataObject are created by a form in the frontend or backend.

    I think it should work like this:
    open the page for the detailed DataObject --> get the ID of the DataObject to display -->get the data out of the database

    How can I realize this ?

    I hope you understand my issue...

  • Hamish
    Avatar
    Community Member
    712 Posts

    Re: Display a DataObject on a new "own" page Link to this post

    Hi,

    You will need to create an action on your page controller. This action will tell silverstripe which DataObject to show and how to show it.

    class ObjectDisplayPage extends Page {

    }

    class ObjectDisplayPage _Controller extends Page_Controller {

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

    }

    Create a new "Object Display Page". Now when you navigate to the page url, you can add /display/5 (for example) to the url.

    "display" is the action on the URL. "5" is the ID url parameters.

    The function will do the following:
    1. Retrieve the dataobject
    2. Test that it exists.
    3. If doesn't exist, redirect the user to a 'not-found' page
    4. If it DOES exist, first customise the current output so the object "DisplayObject" has your object
    5. then, render the page using the template "DisplayObjectPage"

    Now you jsut need to create the template 'DisplayObjectPage'. This template will have the output you want to see, eg:

    <% control DisplayObject %>
    $Title
    $Description
    ...
    <% end_control %>

  • Stefdv
    Avatar
    Community Member
    110 Posts

    Re: Display a DataObject on a new "own" page Link to this post

    Hamish,

    This is possibly not the right way to do it ( i don't mean to 'bypass' Pigeon) but could you please also take a look at my question in this post? http://www.silverstripe.org/data-model-questions/show/255257?start=0#post276408

    I thought you used a different approach to the problem (at least...for me it's a problem ) but it seems you use the same method.

    Tx already

  • LinseyM
    Avatar
    Community Member
    99 Posts

    Re: Display a DataObject on a new "own" page Link to this post

    Hi there,

    Sorry to hijack the thread but I'm trying to do the same thing: so is this the recommended way to do it? Am not too worried about friendly URLs as its only a small part of the site.

    I was working through the "Job Posting" part of the book as I was trying to do something similar, but then it just lost me!!!

    Thanks,

  • edk
    Avatar
    Community Member
    39 Posts

    Re: Display a DataObject on a new "own" page Link to this post

    Hamish is spot on for the down and dirty approach here (by that I mean - not seo friendly in the URL)... For additional details on this method check out the two articles on SSBits:

    1. http://www.ssbits.com/using-silverstripe-url-parameters-to-display-dataobjects-on-a-page/
    2. http://www.ssbits.com/display-dataobjects-in-an-seo-friendly-way/

    Article 2 extends the steps in the first article to turn it into a more SEO friendly URL.

    If you haven't already fire up your RSS reader for SSBits http://www.ssbits.com/...it is a great resource for tips etc on Silverstripe.

  • LinseyM
    Avatar
    Community Member
    99 Posts

    Re: Display a DataObject on a new "own" page Link to this post

    Hi there,

    Thanks for those article links - the first one was really helpful... was struggling to get this to work without understanding what the code was doing. Got it now.

    Much appreciated.

  • timwjohn
    Avatar
    Community Member
    98 Posts

    Re: Display a DataObject on a new "own" page Link to this post

    Does the nested URL support in 2.4 allow for multiple parameters to be passed?

    For example, I want to display all items in a given list of categories, would I be able to set something up like:

    /itemsbycategory/2/6/15

    I understand that this doesn't reflect the true sense of nested URLs, but is it still possible? It's also not necessary (in this project) to have friendly URL, so the ID method is fine by me.

  • timwjohn
    Avatar
    Community Member
    98 Posts

    Re: Display a DataObject on a new "own" page Link to this post

    I guess that you can't have more than 2 parameters as URL segments then. Only 'action' and 'id'. I admit I haven't fully investigated yet, so I'll post back if I find out otherwise.

    One thing that I got stuck on for anyone else going for Hamish's method - don't forget to add the name of the function to the $allowed_actions array in your page controller class.

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