Jump to:

7939 Posts in 1472 Topics by 944 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » Multiple Images Using DataObjectManager and DataObjects as pages

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Page: 1
Go to End
Author Topic: 536 Views
  • ShadeFrozen
    Avatar
    Community Member
    10 Posts

    Multiple Images Using DataObjectManager and DataObjects as pages Link to this post

    Hi,

    I have everything working satisfactorily using SS 2.4 (and am about to implement the UploadField Patch) but I do have one question about the below code. Currently I can only attach one image at a time to a news item. So my question is; how to attach multiple images to a single news item in one action:

    NewsListingPage.php
    ------------------------------------

    <?php
    /**
    * Defines the News Listing Page type
    */
    class NewsListingPage extends DataObjectAsPageHolder
    {

    }

    class NewsListingPage_Controller extends DataObjectAsPageHolder_Controller
    {
    //The Class of the DataObject you want this page to list
    static $item_class = 'NewsItem';
    //Set the sort for the items (defaults to Created DESC)
    static $item_sort = 'Title ASC';
    }

    ?>

    NewsItem.php
    -------------------------

    <?php
    /**
    * Defines the NewsItem DataObject type
    */
    class NewsItem extends DataObjectAsPage {

    //The class of the page which will list this DataObject
    static $listing_class = 'NewsListingPage';
       
    //Class Naming (optional but recommended)
    static $plural_name = 'News Items';
    static $singular_name = 'News Item';

    //Relations
       //add the image(s)
       static $has_many = array (
          'NewsImages' => 'NewsImage'
       );

       public static $db = array (
       );
       
       public static $has_one = array (
       );
       
    //Popup window for adding/editing
       public function getCMSFields_forPopup() {
          $fields = parent::getCMSFields_forPopup();
          //add the (image) DataObject
          $manager = new ImageDataObjectManager(
             $this, // Controller
             'NewsImages', // Source name
             'NewsImage', // Source class
             'MyNewsImage', // File name on DataObject
             array('NewsImageTitle' => 'NewsImageTitle'), // Headings
                'getCMSFields_forPopup' // Detail fields (function name or FieldSet object)
                // Filter clause
                // Sort clause
                // Join clause
             );
          $fields->addFieldToTab('Root.Content.NewsImage', $manager);
          return $fields;

       }
       
    }

    ?>

    NewsImage.php
    -----------------------

    <?php

    class NewsImage extends DataObject
    {
    static $db = array (
    'NewsImageTitle' => 'Varchar(100)'
    );
    static $has_one = array (
    'MyNewsImage' => 'Image', //relation needed for the DataObject
    'BelongToEventPage' => 'NewsItem' //relation pointing to pagetype/dataobject.
    );

    public function getCMSFields_forPopup()
    {
    return new FieldSet(
    new TextField('NewsImageTitle','News Image Title'),
    new ImageUploadField('MyNewsImage')
    );
    }

    }

    ?>

    Any suggestions and/or improvements are welcomed.

    ***********

    TIA
    Mark

  • ShadeFrozen
    Avatar
    Community Member
    10 Posts

    Re: Multiple Images Using DataObjectManager and DataObjects as pages Link to this post

    Any ideas people? Uncle Cheese did suggest this was by design and I needed to either extend the NewsImage class with extra_images OR use the normal DOM and not the imageDOM but I cannot figure it out.

    To be quite honest I am trying to get my head around this as I don't understand why the above isn't working properly. And yes it It works, but as I said, only by adding one image at a time.

    536 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.