Jump to:

7938 Posts in 1541 Topics by 945 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » Displaying content from DataObject in .ss templates

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Page: 1
Go to End
Author Topic: 849 Views
  • Fantastic Contraption
    Avatar
    Community Member
    3 Posts

    Displaying content from DataObject in .ss templates Link to this post

    I've created a DataObject called SideModules for adding custom content to various pages around my SilverStripe site, but at the moment, I'm only using it on my StaffPage. I can create the modules fine in the admin interface, I just can't seem to get them to display in my templates. Just for reference, I'm using SilverStripe 2.4.1.

    SideModule.php

    <?php
    class SideModule extends DataObject
    {
       static $db = array (
          'Title' => 'Text',
          'Content' => 'HTMLText'
       );

       static $belongs_many_many = array(
    'StaffPages' => 'StaffPage'
    );

       public function getCMSFields_forPopup()
       {
          return new FieldSet(
             new TextField('Title'),
             new SimpleHTMLEditorField('Content')
          );
       }
    }
    class SideModule_Controller extends Page_Controller {
    }
    ?>

    StaffPage.php

    <?php
    class StaffPage extends Page {
    static $db = array();

    static $many_many = array(
    'SideModules' => 'SideModule'
    );

    function getCMSFields() {
    $fields = parent::getCMSFields();

    // Add a Data Object Manager to include content on the sidebar
    $fields->addFieldToTab("Root.Content.SideModules", new DataObjectManager(
             $this,
             'SideModules',
             'SideModule',
             array('Title'=>'Title','Content' => 'Content'),
             'getCMSFields_forPopup'
          ));

    return $fields;
    }
    }

    class StaffPage_Controller extends Page_Controller {
    }
    ?>

    StaffPage.ss

    <% if SideModules %>
    <% control SideModules %>
    <div class="section">
    <h2>$Title</h2>
    $Content
    </div>
    <% end_control %>
    <% end_if %>

    So, everything works on the admin side of things as far as I can tell. I can add SideModules to a StaffPage template. I can re-order, edit and delete them. However, the SideModules control just doesn't output anything. It doesn't throw any errors or anything. Any ideas where I'm going wrong? There is also a StaffHolder page for displaying all StaffPage items, but I dont' think that has anything to do with the problems I'm having getting the content to display.

  • UncleCheese
    Avatar
    4085 Posts

    Re: Displaying content from DataObject in .ss templates Link to this post

    Yeah, you've kind of messed things up. I'm not sure what SideModule_Controller is, but it definitely doesn't belong there. DataObjects don't have controllers. That's why you need to do custom coding to get them to display like pages.

    To manage the many_many in your StaffPage, you need a ManyManyDataObjectManager, not a regular DataObjectManager.

    --------------------
    SilverStripe tips, tutorials, screencasts and more: http://www.leftandmain.com

  • Fantastic Contraption
    Avatar
    Community Member
    3 Posts

    Re: Displaying content from DataObject in .ss templates Link to this post

    Cheers for the reply UncleCheese, your suggestions got me thinking about how I had set everything up and I've managed to fix everything. The many-many relationship was the wrong choice for one, so I've gone for one to many as it makes the most sense for what I need.

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