Silverstripe Uploadify Add fields

后端 未结 1 1196
独厮守ぢ
独厮守ぢ 2020-12-19 05:11

I am looking to attach multiple images to a page in silverstripe (that will act as a rotating gallery eventually) and was wondering if it was possible for a user to add a li

1条回答
  •  醉梦人生
    2020-12-19 05:38

    Yes. This can be achieved by having a has_many relationship to a custom DataObject which contains an image and a link object in it.

    In the following example we have a HomePage that has a has_many relationship to Slide. Slide contains an Image and Link.

    There is some good information on this topic in SilverStripe Lesson 9 - Working with data relationships - $has_many.

    The optional sorting module I use in this example is the SortableGridField.

    SilverStripe 3.1

    Slide.php

    class Slide extends DataObject {
    
        private static $db = array(
            'Title' => 'Text',
            'SortOrder' => 'Int'
        );
    
        private static $has_one = array(
            'HomePage' => 'HomePage', 
            'Image' => 'Image', 
            'Link' => 'SiteTree'
        );
    
        private static $field_labels = array( 
            'Image.CMSThumbnail' => 'Image', 
            'Link.Title' => 'Link'
        ); 
    
        private static $summary_fields = array( 
            'Title',
            'Image.CMSThumbnail', 
            'Link.Title'
        ); 
    
        private static $default_sort = 'SortOrder ASC';
        private static $singular_name = 'Slide';
        private static $plural_name = 'Slides';
    
        public function getCMSFields() {
    
            $image = UploadField::create('Image', 'Image');
            $image->setFolderName('slides');
    
            $fields = FieldList::create(
                TextField::create('Title'), 
                $image, 
                TreeDropdownField::create('LinkID', 'Link', 'SiteTree')
            );
            return $fields;
        }
    
    }
    

    HomePage.php

    class HomePage extends Page {
    
        private static $has_many = array(
            'Slides' => 'Slide'
        );
    
        public function getCMSFields() {
            $fields = parent::getCMSFields();  
    
            $slidesFieldConfig = GridFieldConfig_RecordEditor::create();
    
            // The following requires the SortableGridField module installed
            $slidesFieldConfig->addComponent(new GridFieldSortableRows('SortOrder'));
    
            $slidesField = GridField::create(
                'Slides',
                'Slide',
                $this->Slides(),
                $slidesFieldConfig
            );
    
            $fields->addFieldToTab('Root.Slides', $slidesField);
    
            return $fields;
        }
    
    }
    

    Layout/HomePage.ss

    <% if $Slides %>
        
    <% loop $Slides %> <% if $Link %> $Title <% else %>
    $Title
    <% end_if %> <% end_loop %>
    <% end_if %>

    SilverStripe 2.4

    Using DataObjectManager and Uploadify modules:

    Slide.php

    class Slide extends DataObject {
    
        static $db = array(
            'Title' => 'Text'
        );
    
        static $has_one = array(
            'HomePage' => 'HomePage', 
            'SlideImage' => 'Image', 
            'Link' => 'SiteTree'
        );
        static $singular_name = 'Slide';
        static $plural_name = 'Slides';
    
        public function getCMSFields_forPopup()
        {
            return new FieldSet(
                new TextField('Title'),
                new SimpleTreeDropdownField('LinkID', 'Slide Link', 'SiteTree'),
                new ImageUploadField('SlideImage', 'Slide Image')
            );
        }
    
    }
    

    HomePage.php

    class HomePage extends Page {
    
        public static $has_many = array(
            'Slides' => 'Slide'
        );
    
        public function getCMSFields()
        {
            $fields = parent::getCMSFields();
    
            $slideManager = new ImageDataObjectManager(
                $this,
                'Slides',
                'Slide',
                'Image', 
                array(
                ),
                'getCMSFields_forPopup'
            );
            $fields->addFieldToTab('Root.Content.Slides', $slideManager);
    
            return $fields;
        }
    
    }
    

    Layout/HomePage.ss

    <% if Slides %>
        
    <% control Slides %> <% if Link %> $Title <% else %>
    $Title
    <% end_if %> <% end_control %>
    <% end_if %>

    0 讨论(0)
提交回复
热议问题