本文介绍了Silverstripe Uploadify添加字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望将多个图像附加到silverstripe中的页面(最终将充当旋转图库)并且想知道用户是否可以在上传每个图像时为每个图像添加链接? / p>

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?

推荐答案

是的。这可以通过将 has_many 与自定义 DataObject 的关系实现,其中包含图像和链接对象。

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.

在以下示例中,我们有一个 HomePage ,其 has_many 的关系。 幻灯片包含图像链接

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

在。

我使用的可选排序模块这个例子是。

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

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

}

布局/主页。 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



使用DataObjectManager和Uploadify模块:

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

}

布局/主页。 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 %>

这篇关于Silverstripe Uploadify添加字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 22:37