'Silverstripe 4 SiteConfig logo image was not showed in template
I create custom field for logo upload in SiteConfig and after uploading logo in Settings and trying to render it in .ss template. After rendering nothing was showed in template. This code i use in Silverstripe v3 and all work good. Now in v4 not work.
Here is my 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");
}
}
Here is my template file 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 %>
What i miss? What i do wrong ? Thanks for answer.
Solution 1:[1]
Images are versioned in SilverStripe 4, so you'll need to ensure that the image gets published when you save your SiteConfig object.
You don't mention which version of SilverStripe 4 you're using - this issue has been looked at in 4.1.2 and 4.2.0-beta1 so far. This would mean that if you apply the ownership API to these related objects then they will be automatically published when the SiteConfig model is saved, e.g.:
private static $owns = ['Logo', 'DefaultImage'];
If you're using SilverStripe 4.1.2 or newer then the above is all you'll need to do.
For versions earlier than this, you can implement your own hook in your your SiteConfigExtension:
public function onAfterWrite()
{
if ($this->owner->Logo()->exists()) {
$this->owner->Logo()->publishSingle();
}
// ... same for other has_ones that are versioned
}
Solution 2:[2]
In SilverStripe version 4 the scaling for images in the templates has been changed and SetWidth is no longer used in SilverStripe 4.
In version 3 the scaling was done via
SetWidth(200)
In version 4 this changed to
ScaleWidth(200)
If you change this in your template this might fix your problem.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | scrowler |
| Solution 2 | Samuto |
