我为SiteConfig中的logo上传创建了自定义字段,并在Settings中上传徽标并尝试在.ss模板中呈现徽标之后。渲染后,模板中没有显示任何内容。我在Silverstripe v3中使用的这段代码都很好用。现在在v4不工作了。
这是我的Extension**:**
class SiteConfigExtension extends \SilverStripe\ORM\DataExtension
{
private static $db = array (
'FacebookLink' => 'Varchar',
'TwitterLink' => 'Varchar',
'GoogleLink' => 'Varchar',
);
private static $has_one = array(
'Logo' => Image::class,
'DefaultImage' => Image::class
);
public function updateCMSFields(FieldList $fields)
{
parent::updateCMSFields($fields);
$fields->addFieldsToTab('Root.Social', array (
TextField::create('FacebookLink','Facebook'),
TextField::create('TwitterLink','Twitter'),
TextField::create('GoogleLink','Google'),
));
$fields->addFieldsToTab('Root.Main', array(
$logo = UploadField::create('Logo', 'Logo'),
$defaultImage = UploadField::create('DefaultImage', 'Default Image'),
));
$logo->setFolderName('Logo');
$defaultImage->setFolderName("Settings");
}
}这里是我的模板文件header.ss**:**
<% with $SiteConfig %>
<div style="display: inline-block;">
<div style="float: left;">
<h1 id="logo">
<% if $Logo %>
<a>$Logo.SetWidth(50)</a>
<% end_if %>
</h1>
</div>
<div id="logo-tagline" style="float:left;">
<% if $Title %>
<h1>$Title</h1>
<% end_if %>
<% if $Tagline %>
<strong>$Tagline</strong>
<% end_if %>
</div>
</div>
<% end_with %>我错过了什么?我做错了什么?谢谢你的回答。
发布于 2018-06-26 22:45:15
图像是在SilverStripe 4中进行版本化的,因此您需要确保在保存SiteConfig对象时图像被发布。
您没有提到您正在使用的SilverStripe 4的哪个版本- 本期到目前为止已经在4.1.2和4.2.0-beta1中被研究过。这意味着,如果将所有权API应用于这些相关对象,则在保存SiteConfig模型时将自动发布它们,例如:
private static $owns = ['Logo', 'DefaultImage'];如果您使用的是SilverStripe 4.1.2或更高版本,那么您只需要执行上面的操作。
对于早于此的版本,可以在SiteConfigExtension中实现自己的钩子:
public function onAfterWrite()
{
if ($this->owner->Logo()->exists()) {
$this->owner->Logo()->publishSingle();
}
// ... same for other has_ones that are versioned
}发布于 2022-05-19 09:33:50
在SilverStripe版本4中,模板中图像的缩放已经更改,SetWidth不再在SilverStripe 4中使用。
在第3版中,缩放是通过
SetWidth(200)在第4版中,这更改为
ScaleWidth(200)如果在模板中更改此内容,可能会解决问题。
https://stackoverflow.com/questions/51042595
复制相似问题