Jump to:

5450 Posts in 1672 Topics by 1197 members

Customising the CMS

SilverStripe Forums » Customising the CMS » Page categories with icons/images

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

Page: 1
Go to End
Author Topic: 1113 Views
  • neglostyti
    Avatar
    Community Member
    5 Posts

    Page categories with icons/images Link to this post

    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.

  • martimiz
    Avatar
    Forum Moderator
    1037 Posts

    Re: Page categories with icons/images Link to this post

    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 {
    }

  • neglostyti
    Avatar
    Community Member
    5 Posts

    Re: Page categories with icons/images Link to this post

    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

    1113 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.