我希望将多个图像附加到silverstripe中的页面(最终将充当旋转图库)并且想知道用户是否可以在上传每个图像时为每个图像添加链接? / p>
答案 0 :(得分:13)
是。这可以通过与自定义has_many
建立DataObject
关系来实现,其中包含图片和链接对象。
在以下示例中,我们有HomePage
与has_many
的{{1}}关系。 Slide
包含Slide
和Image
。
SilverStripe Lesson 9 - Working with data relationships - $has_many中有关于此主题的一些很好的信息。
我在本例中使用的可选排序模块是SortableGridField。
<强> Slide.php 强>
Link
<强> HomePage.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.ss 强>
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;
}
}
使用DataObjectManager和Uploadify模块:
<强> Slide.php 强>
<% 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 %>
<强> HomePage.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.ss 强>
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;
}
}