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.

General Questions /

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

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

Help with displaying HasManyComplexTableField data on frontend


Reply


794 Views

Avatar
bronks

Community Member, 2 Posts

5 August 2010 at 1:32pm

I'm trying to filter data objects to display on the front-end of my home page. I created three functions within my HomePage controller to filter categories. However nothing is displaying on the frontend. My code below:

Resource.php:

class Resource extends DataObject {
   static $db = array (
      'Name' => 'Text',
      'FileName' => 'Text',
      'Category' => "Enum('Program Details, Photograph Collections, Videos')"
   );
   public static $has_one = array(
      'MyPage' => 'HomePage'
   );

   public function getCMSFields_forPopup() {
      $fields = new FieldSet();
      $fields->push( new TextField( 'Name', 'Name' ) );
      $fields->push( new TextField( 'FileName', 'File Name' ) );
      $fields->push( new DropdownField('Category','Category', singleton('Resource')->dbObject('Category')->enumValues()) );
      return $fields;
   }

Homepage.php:

class HomePage extends Page {

   public static $db = array(
   );
   static $has_many = array (
      'Resources' => 'Resource'
   );
   
   function getCMSFields() {
      $fields = parent::getCMSFields();

      $tablefield = new HasManyComplexTableField(
         $this,
         'Resources',
         'Resource',
         array(
            'Name' => 'Name',
            'FileName' => 'File Name',
            'Category' => 'Category'
         ),
         'getCMSFields_forPopup'
      );
      $tablefield->setParentClass('HomePage');
      $tablefield->setAddTitle('A Resource');

      $fields->addFieldToTab( 'Root.Content.Resources', $tablefield );
      
      return $fields;
   }
}
class HomePage_Controller extends Page_Controller {

   public function ProgramDetails() {
      return $this->Level(1)->Resources("Category = 'Program Details'");
   }
   public function PhotographCollections() {
      return $this->Level(1)->Resources("Category = 'Photograph Collections'");
   }
   public function Videos() {
      return $this->Level(1)->Resources("Category = 'Videos'");
   }
}

Homepage.ss:

<% if ProgramDetails %>
<ul>
   <% control ProgramDetails %>
   <li>$Name</li>
   <% end_control %>
</ul>
<% end_if %>

Help!!