Making ‘News’ Page with Addons in SilverStripe CMS Part 2

Now we add some addons to news article page. Like article Image and Gallery.

  1. For Image. Open ArticlePage.php in mysite/code/ and change lines:
    static $db = array(
           'Date' => 'Date',
           'Author' => 'Text',
    );
    
    to:
    
    static $db = array(
           'Date' => 'Date',
           'Author' => 'Text',
    );
    static $has_one = array ( 
           'Photo' => 'Image'
    );
    static $has_many = array (
    	'NewsGalleryImages' => 'NewsGalleryImage'
    );
    
    AND
    
    function getCMSFields() {
        $fields = parent::getCMSFields();
        $fields->addFieldToTab('Root.Content.Main', $dateField = new DateField('Date','Article Date (for example: 20/12/2010)'), 'Content');
        $dateField->setConfig('showcalendar', true);
        $dateField->setConfig('dateformat', 'dd/MM/YYYY');
        $fields->addFieldToTab('Root.Content.Main', new TextField('Author','Author Name'), 'Content');
        return $fields;
    }
    
     to:
    
    function getCMSFields() {
        $fields = parent::getCMSFields();
        $fields->addFieldToTab('Root.Content.Main', $dateField = new DateField('Date','Article Date (for example: 20/12/2010)'), 'Content');
        $dateField->setConfig('showcalendar', true);
        $dateField->setConfig('dateformat', 'dd/MM/YYYY');
        $fields->addFieldToTab('Root.Content.Main', new TextField('Author','Author Name'), 'Content');
        $fields->addFieldToTab("Root.Content.Main", new ImageField('Photo'));
        $fields->addFieldToTab("Root.Content.Main", new ImageDataObjectManager($this,'NewsGalleryImages','NewsGalleryImage','NewsMyGalleryImage'));
        return $fields;
    }
  2. Make file NewsGalleryImage.php to folder mysite/code/ using code:
    <?php
    class NewsGalleryImage extends DataObject {
       static $db = array (
       );
       static $has_one = array (
          'NewsMyGalleryImage' => 'Image',
          'BelongToArticlePage' => 'ArticlePage'
       );
    
       public function getCMSFields_forPopup()
       {
          return new FieldSet(
             new FileIFrameField('NewsMyGalleryImage'),
          );
       }
    }
    ?>
  3. Now add codes to your ArticlePage.ss:
    For photo use:
    $Photo.SetHeight(200)
    
    For Gallery use:
    <% control NewsGalleryImages %>
    <% control NewsMyGalleryImage %>
    <a href="$SetHeight(500).URL" >$SetHeight(100)</a>
    <% end_control %> 
    <% end_control %>
  4. Rebuild your database using www.yoursitename.com/dev/build?flush=1

Looking for quality SilverStripe Web Hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , , , , , , , , , , , | Posted under SilverStripe CMS | RSS 2.0

Author Spotlight

Teet Bergmann

Teet Bergmann

I'm a freelance web designer from Estonia. Mostly doing sites using Silverstripe, magento, prestashop, wordpress.

Leave a Reply

Your email address will not be published. Required fields are marked *


5 − = 3

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>