Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » Add custom content to the system

Our old forums are still available as a read-only archive.

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

Page: 1 2 3
Go to End
Author Topic: 4693 Views
  • Mednezz
    Avatar
    Community Member
    23 Posts

    Add custom content to the system Link to this post

    Hey Guys!

    I've made a certain pagetype with my own set of data fields. The administrator can add the pages without any problem using the cms. But now i've got a private public section where logged in visitors should also be able to add those kind of pages.

    When building the DB SS has made 3 tables :

    [pagetype]Page
    [pagetype]Page_Live
    [pagetype]Page_versions

    Now i've made a form containing al the fields needed and when i process that form i could create an object $obj = new [pagetype]Page; and then $form->saveInto($obj);

    But! ;-)

    I also need it to get into the sitetree so the page can be called with an url.

    Finally my questions..

    Are there functions inside SS so i can automatically publish the latest added page to the _Live table and how should i get it into the SiteTree / SiteTree_Live

    Hopefully i'm clear enough!

    Thanks in advance!

  • dio5
    Avatar
    Community Member
    501 Posts

    Re: Add custom content to the system Link to this post

    I'm going to face more or less the same problem in the next few weeks somewhere... and keen on seeing someone else's solution to this too.

    In my site people will be adding stuff via the frontend that has to become a page in the sitetree.

    So I'll have a class 'Tip', and logged in users will be able to submit a tip... not a problem, but every tip is a subpage of tipholderpage in the cms-sitetree too...

  • Mednezz
    Avatar
    Community Member
    23 Posts

    Re: Add custom content to the system Link to this post

    Yep, same thing here, my to be added pages are also a child of an holder.

    Have found some functions in /sapphire/core/model/versioned and especially function publish() ;)

    But would like to ask the core dudes what the best approach would be

    Curious to what they have to say!

  • Ingo
    Avatar
    Forum Moderator
    801 Posts

    Re: Add custom content to the system Link to this post

    yeah, you should use $myPageObj->publish() for that.
    for getting the page into the sitetree, i'd use the URLSegment to get the holder,
    and set $myPageObj->ParentID.
    you might want to add additional security in onBeforeWrite() to make sure nobody will add certain fields, or change the pagetype etc.:

    class MyPage extends Page {
    function onBeforeWrite() {
    // if record is created
    if(!$this->ID) {
    // if no valid login
    if(!Member::currentUser()) {
    // filter $this->record-array for allowed values
    }
    }
    parent::onBeforeWrite();
    )


    alternatively, this can be done in the form-saving code.

  • dio5
    Avatar
    Community Member
    501 Posts

    Re: Add custom content to the system Link to this post

    @Mednezz (or anyone else having done this)

    I was wondering if you managed to do it eventually?

    In the form-saving function do you actually create a new Page and new SiteTree too? I mean how did you handle the fields like URLSegment, MenuTitle...

    If you could point me to an example that could get me started, that would be fantastic.

  • dio5
    Avatar
    Community Member
    501 Posts

    Re: Add custom content to the system Link to this post

    What I have now seems to add to Tip_Live and SiteTree_Live, but doesn't seem to pass it correctly to Tip and SiteTree.
    Moreover it gives a fatal error...

    What I have basically in the save-function of my form is:

    $tip = new Tip();
             
    $tip->Street = $data['Street'];
    $tip->StreetNumber = $data['StreetNumber'];
    // a lot of tip properties...
       
    // Then I set the properties inherited from SITETREE
    // Do I have to set ClassName? Do I have to set ViewersGroup?

    $tip->Title = $data['Title'];
    $tip->Content = "test";
    $tip->ClassName = "Tip";
    $tip->Status = "Published";
    $tip->Viewers = "Anyone";
    $tip->ParentID = 2;
          
    /* And here I have to write it to all the tables.. and this is where I'm doing something wrong, but I'm not sure what or how to do it, can't find any documentation about it...:*/

    $tip->write();
    $tip->writeToStage("Stage"); // Is this correct?
    $tip->publish("Stage", "Live"); // this?

    I checked out the blog-code and based my code a bit on that one.. but obviously something is wrong.

    the fatal error was:
    FATAL ERROR: Can't find testtitle/22 in stage Stage
    At line 300 in D:\htdocs\silv21rc\sapphire\core\model\Versioned.php

    As mentioned before: the data is in the tables Tip_Live and SiteTree_Live, but not in Tip and SiteTree...

  • dio5
    Avatar
    Community Member
    501 Posts

    Re: Add custom content to the system Link to this post

    If only someone could provide some answers to these, probably not even hard, questions...??

  • Ingo
    Avatar
    Forum Moderator
    801 Posts

    Re: Add custom content to the system Link to this post

    usually in a method called by a form, you'll have two parameters: $data and $form.
    based on the $data, you usually just get an object that needs to be altered (e.g. through $data['ID']). alternatively you create a new object, same thing.

    then you just call $form->saveInto($myObject), so you don't have to do all the property-setting yourself. just another $myObject->publish() call should be needed in your case (the writing to stage should happen automatically afaik)

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