在这里感觉有点愚蠢,我希望这只是 SS4 中我不知道的新事物,而不仅仅是我的高龄让我变得更好......
我的 DataObject 中有一个 UploadField。但是当我在模板中循环浏览它时,我无法显示图像。
我的页面.php:
<?php
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Forms\GridField\GridField;
use SilverStripe\Forms\GridField\GridFieldConfig_RecordEditor;
use UndefinedOffset\SortableGridField\Forms\GridFieldSortableRows;
class MyPage extends Page {
private static $has_many = array(
'MyObjects' => MyObject::class
);
...
我的对象.php:
<?php
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\ORM\DataObject;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\TabSet;
use SilverStripe\Forms\TextField;
use SilverStripe\AssetAdmin\Forms\UploadField;
use SilverStripe\Assets\Image;
use SilverStripe\AssetAdmin\Forms\PreviewImageField;
class MyObject extends DataObject {
private static $db = array(
'Title' => 'Varchar(255)'
);
private static $has_one = array(
'MyImage' => Image::class,
'MyPage' => MyPage::class
);
public function getGeneratedCMSFields() {
$fields = FieldList::create(TabSet::create('Root'));
$fields->addFieldToTab('Root.Main', TextField::create('Title', 'Title'));
$fields->addFieldToTab('Root.Main', $img = UploadField::create('MyImage', 'My Image'));
$img->getValidator()->setAllowedExtensions = array('jpg', 'jpeg', 'gif', 'png');
$fields->removeFieldFromTab('Root.Main', 'MyPageID');
return $fields;
}
public function getCMSFields() {
$fields = $this->getGeneratedCMSFields();
return $fields;
}
}
我的页面.ss:
<% loop $MyObjects %>
<h1>$Title</h1>
<% with $MyImage %><img src="{$URL}" title="{$Up.Title}"><% end_with %>
<% end_loop %>
这会按预期打印出 $Title 和 alt img 标签,但没有图像。我什至尝试将复杂性降低到简单的 <% loop $MyObjects %> $MyImage <% end_loop %> 并且仍然没有图像。我错过了什么?