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.

Customising the CMS /

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

Accessing Dataobject on home page


Go to End


3 Posts   1104 Views

Avatar
shuey79

Community Member, 3 Posts

18 February 2015 at 6:38am

I'm new to programming and need a little (probably a lot) of help. I've been following a bunch of tutorials but can't seem to find what I want. I'm trying to extend my site to display testimonies on the homepage but can't figure out how to do it. I think I need a TestimonyHolder page to display the testimonies but am unsure of how I would access the dataobject there.
I am using ModelAdmin to display a tab in the admin section so a user can populate testimonies.
MyAdmin.php:

<?php
class MyAdmin extends ModelAdmin {

    private static $managed_models = array(
        'Testimonial'
    );
    private static $url_segment = 'testimonials';
    private static $menu_title = 'Testimonials';   
}

This is my Testimonial page:

<?php

class Testimonial extends DataObject {

  private static $db = array(
        'Date' => 'Date',
        'Name' => 'Varchar',
        'Business' => 'Varchar',
        'Testimony' => 'Text'
        
	);

  public function getCMSFields() {
        $fields = parent::getCMSFields();
        
        $dateField = new DateField('Date', 'Testimonial Date (for example: 12/20/2010)');
        $dateField->setConfig('showcalendar', true);
        $dateField->setConfig('dateformat', 'MM/dd/YYYY');
	$fields->replaceField('Date', $dateField);
	
        return $fields;
  }

  public function canDelete($member = null) {
	return Permission::check('CMS_ACCESS_MyAdmin', 'any', $member);
  }
}

Thanks in advance!

Avatar
g4b0

Community Member, 14 Posts

18 February 2015 at 9:12pm

You have to create a HomePage class that extends Page, with a function that returns the testimonials:

class HomePage extends Page {
	public function Testiomonials() {
		return Testimonial::get()
	}
}

class HomePage_Controller extends Page_Controller {
	
}

After that, in your template file (eg. HomePage.ss) you have to loop over the Testimonials:

<ul>
<% loop $Testiomonials %>
	<li>$Name</li>
<% end_loop %>
</ul>

Hope it helps
g4b0

Avatar
shuey79

Community Member, 3 Posts

19 February 2015 at 2:20am

g4b0, that worked out great. thanks!!