我希望将多张图片附加到 silverstripe 中的页面(最终将充当旋转画廊),并且想知道用户在上传每张图片时是否可以为每张图片添加链接?
问问题
1972 次
1 回答
13
是的。这可以通过与包含图像和链接对象has_many
的自定义建立关系来实现。DataObject
在下面的示例中,我们有一个HomePage
与 有has_many
关系Slide
。Slide
包含一个Image
和Link
。
SilverStripe 第 9 课 - 处理数据关系 - $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 %>
于 2013-01-21T05:03:18.650 回答