Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
576 views
in Technique[技术] by (71.8m points)

image - Silverstripe Uploadify Add fields

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 link for each of these images when uploading each image?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 %>
    <div class="slideshow">
        <% loop $Slides %>
            <% if $Link %>
                <a class="slide" href="$Link.URL" title="Go to the $Link.Title.XML page">
                    <img src="$Image.URL" alt="$Title" />
                </a>
            <% else %>
                <div class="slide">
                    <img src="$Image.URL" alt="$Title" />
                </div>
            <% end_if %>
        <% end_loop %>
    </div>
<% 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 %>
    <div class="slideshow">
        <% control Slides %>
            <% if Link %>
                <a class="slide" href="$Link.URL" title="Go to the $Link.Title.XML page">
                    <img src="$Image.URL" alt="$Title" />
                </a>
            <% else %>
                <div class="slide">
                    <img src="$Image.URL" alt="$Title" />
                </div>
            <% end_if %>
        <% end_control %>
    </div>
<% end_if %>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...