gpt4 book ai didi

image - Silverstripe Uploadify 添加字段

转载 作者:行者123 更新时间:2023-12-04 06:05:33 26 4
gpt4 key购买 nike

我希望将多张图片附加到 silverstripe 中的页面(最终将充当旋转画廊),并且想知道用户在上传每张图片时是否可以为每张图片添加链接?

最佳答案

是的。这可以通过 has_many 来实现与风俗的关系DataObject其中包含一个图像和一个链接对象。

在以下示例中,我们有一个 HomePage有一个 has_manySlide 的关系. Slide包含 ImageLink .

SilverStripe Lesson 9 -Working with data relationships - $has_many 中有一些关于这个主题的好信息.

我在这个例子中使用的可选排序模块是 SortableGridField .

银条 3.1

幻灯片.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;
}

}

首页.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 %>

银条 2.4

使用 DataObjectManager 和 Uploadify 模块:

幻灯片.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')
);
}

}

首页.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 %>

关于image - Silverstripe Uploadify 添加字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14432739/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com