Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Displaying content from DataObject in .ss templates


Go to End


3 Posts   1274 Views

Avatar
Fantastic Contraption

Community Member, 3 Posts

14 October 2010 at 4:54am

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.

Avatar
UncleCheese

Forum Moderator, 4102 Posts

14 October 2010 at 5:12am

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

Avatar
Fantastic Contraption

Community Member, 3 Posts

14 October 2010 at 6:00am

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.