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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Testimonials Page issue with SS 3.1


Go to End


4 Posts   576 Views

Avatar
helenclarko

Community Member, 166 Posts

13 November 2014 at 11:35am

Edited: 17/11/2014 8:52am

Hi All,

I'm working on updating a testimonial page from SS 2.4 to 3.1, I just cant seem to make it display by type.

So far I have the following:

TestimonialPage.php

<?php
class TestimonialPage extends Page {

	public static $db = array(
	);

	public static $has_one = array(
	);
	
	public static $has_many = array(
		'BigTestimonials' => 'BigTestimonials'
	);
	
	function getCMSFields() {
		$fields = parent::getCMSFields();
		
		//Testimonials
		$gridFieldConfig = GridFieldConfig::create()->addComponents(
		  new GridFieldToolbarHeader(),
		  new GridFieldAddNewButton('toolbar-header-right'),
		  new GridFieldSortableHeader(),
		  new GridFieldDataColumns(),
		  new GridFieldPaginator(10),
		  new GridFieldEditButton(),
		  new GridFieldDeleteAction(),
		  new GridFieldDetailForm()
		);
		
        $testimonialTable = new GridField("Testimonials", "Testimonials:", $this->BigTestimonials(), $gridFieldConfig);   
        $fields->addFieldToTab('Root.Testimonials', $testimonialTable);
		
		return $fields;
	}
}

class TestimonialPage_Controller extends Page_Controller {
	public function GetTestimonials(){
		$testimonials = DataObject::Get('BigTestimonials', 'TestimonialPageID = ' . $this->ID);
		$testimonials->groupBy('Type'); 
		
		return $testimonials;	
	}
}

BigTestimonials.php

<?php
class BigTestimonials extends DataObject{
	static $db = array(
		'PersonsName' => 'Text',
		'Testimonial' => 'HTMLText',
		'Type' => 'Varchar(250)'
	);

	static $has_one = array(
		'TestimonialPage' => 'TestimonialPage'
	);
	
	public function getCMSFields() {
		$fields = new FieldList(new TabSet('Root'));
         
		//General info
		$Types = array(
			'Parent' => 'Parent',
			'School and Tutors' => 'School and Tutors',
			'Adult' => 'Adult'
		);
		$fields->addFieldToTab("Root.Info", new DropdownField('Type', 'Select the testimonial type', $Types));
		$fields->addFieldToTab("Root.Info", new TextField('PersonsName', 'Persons name'));
		$fields->addFieldToTab("Root.Info", new HTMLEditorField("Testimonial", "Enter the testimonial"));
         
        return $fields;
	}
}

TestimonialPage.ss

<div id="contentHolder">
	<% include SideBar %>
	
	<div id="content" class="typography">
		$Content
		
		<% if BigTestimonials %>
			<% loop BigTestimonials.GroupedBy(Type) %>
				Type
				<div class="testimonialScrollWrap">
					<h3>$Type Testimonials</h3>
					<div class="testimonialGroupWrap">
						<% loop Children %>
							<div class="eachtestimonial <% if Last %>last<% end_if %>">
								<h5>$PersonsName</h5>
								$Testimonial
							</div>
						<% end_loop%>
					</div>
				</div>
				<hr />
			<% end_loop %>
		<% end_if %>
	</div>
</div>

-helenclarko

Avatar
helenclarko

Community Member, 166 Posts

17 November 2014 at 8:53am

Any help would be appreciated.

Avatar
martimiz

Forum Moderator, 1391 Posts

18 November 2014 at 3:35am

Edited: 18/11/2014 3:37am

In v3 you need to explicitly create a Grouped List. See: http://doc.silverstripe.org/framework/en/howto/grouping-dataobjectsets

Something like this:

class TestimonialPage_Controller extends Page_Controller {

   public function getTestimonials(){
      $list = BigTestimonials::get()->filter(array('TestimonialPageID' => $this->ID));
      return GroupedList::create($list);
   } 	
}

      <% if $BigTestimonials %>
         <% loop $Testimonials.GroupedBy(Type) %>
            ...

Avatar
helenclarko

Community Member, 166 Posts

18 November 2014 at 3:37pm

Thats perfect, Thank you.

Was not expecting to just drop that in and have it instantly work.

Again, Thank you.