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.

Customising the CMS /

Page categories with icons/images


Reply


3 Posts   1260 Views

Avatar
neglostyti

Community Member, 5 Posts

9 March 2011 at 8:38am

Hello,

I'm very new to SS. In fact, I don't know if I'm posting on right Forum:]

I'm trying to create Page Categories with Images. Right now I have Categories class:

I was trying to add Image Field, but it's not worked.

<?php
class Category extends DataObject {

static $db = array(
'Title' => 'Varchar'
// 'Title2' => 'Varchar'
);

static $has_one = array(
// 'Photo' => 'Image'
);

static $field_types = array(
'Title' => 'TextField',
// 'Photo' => 'ImageField'
// 'Photo' => 'ImageField'
);

static $field_names = array(
'Title' => 'Category Name',
// 'Photo' => 'Category Name 2',
// 'Photo' => 'Category Icon'
);

static $belongs_many_many = array(
'Recipes' => 'Recipe'
);

}
?>

and then

<?php
class Page extends SiteTree {
// ..
function getCMSFields() {
$fields = parent::getCMSFields();

$categoryTable = new TableField('Categories', 'Category', Category::$field_names, Category::$field_types);

$categoryTable->setPermissions(array('add', 'edit', 'delete'));

$fields->addFieldToTab('Root.Content.Categories', $categoryTable);
return $fields;
}
// ..
}
?>

without Image it works perfect, but I need add Image icon for each category.

Can same one help me?

Sorry for my bad english, it's not my native language.

Avatar
martimiz

Forum Moderator, 1133 Posts

17 March 2011 at 6:58am

Hi there,

A bit late maybe, and I hope you're still with us :-) Anyway, I wouldn't advise you to use the TableField. For this you'd want the ComplexTableField. A very basic setup for what you're doing. You could take it further from there. Hope this helps some:

class Category extends DataObject {

   static $db = array(
      'Name' => 'Varchar',
      'Title' => 'Varchar'
   );

   static $has_one = array(
      'Photo' => 'Image',
      'CategoryPage' => 'Page'
   );

   static $summary_fields = array(
      'Name',
      'Title'
   );

   public function getCMSFields() {
      $fields = new FieldSet(
         new TextField('Name', 'Category name'),
         new TextField('Title', 'Category title'),
         New ImageField('Photo', 'Category image')
      );
      return $fields;
   }
}

and

class CategoryPage extends Page {

   static $has_many = array(
      'Categories' => 'Category'
   );

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

      $fields->addFieldToTab('Root.Content.Categories',
         $categories = new ComplexTableField (
            $this,
            'Categories',
            'Category'
         )
      );
      return $fields;
   }
}
class CategoryPage_Controller extends Page_Controller {
}

Avatar
neglostyti

Community Member, 5 Posts

17 March 2011 at 7:28am

Hi martimiz,

thanks for your help. I already solved it with ManyManyComplexTableField(); Don't know if it's the best solution, but for now it works as I need:]

Thank again with the replay.

Good luck