SilverStripe 4 beta3 - 来自DataObject的图像未显示在Template.ss中

时间:2017-09-28 23:33:47

标签: php silverstripe silverstripe-4

在这里感觉有点愚蠢,我希望这只是SS4中我不知道的新事物,而不仅仅是我的高龄让我变得更好......

我的DataObject中有一个UploadField。但是当我在模板中循环播放时,我无法显示图像。

MyPage.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
    );
...

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

MyPage.ss:

<% loop $MyObjects %>
    <h1>$Title</h1>
    <% with $MyImage %><img src="{$URL}" title="{$Up.Title}"><% end_with %>
<% end_loop %>

这会按预期打印出$ Title和alt img标签,但没有图像。我甚至试图将复杂性简化为&lt;%loop $ MyObjects%&gt; $ MyImage&lt;%end_loop%&gt;仍然没有形象。我错过了什么?

0 个答案:

没有答案