I created an image in the "ItemPage extends Page" static $has_one array
static $has_one = array( 'BannerImg' => 'Image' );
but I want to be able to use a default image if the image
has not been set. How can I do that?
I am trying to create a function in the "ItemPage_Controller extends Page_Controller"
but I can't determine in the function if "BannerImg" is actually
set. I tried using:
isset($this->BannerImg) always returns true
strlen($this->BannerImg) always returns false
$this->BannerImg.length always returns a non-zero value
sizeof($thisBannerImg) always returns zero
We've moved the forum!
Please use forum.silverstripe.org for any new questions
(announcement).
The forum archive will stick around, but will be read only.
You can also use our Slack channel
or StackOverflow to ask for help.
Check out our community overview for more options to contribute.
[code php]
if ($this->BannerImgID > 0) {
// image is set
} else {
// image is not set
}
or, you can try in the template, but I am not sure, if it works
<% if BannerImg %>
$BannerImg
<% else %>
<img src="default.png"/>
<% end_if %>
<% if BannerImg %>
$BannerImg
<% else %>
<img src=\"default.png\"/>
<% end_if %>
should work fine btw, we use that method quite alot
Thanks
Both methods worked. The second method <% if BannerImg %> is what I really
wanted but I guess as a newbee I was trying to mix <% control Level() %> and
getting it wrong.
On the first method, where is it Documented about the BannerImgID? Do
all created Fields have that (ie. <field>ID)?
"On the first method, where is it Documented about the BannerImgID? Do
all created Fields have that (ie. <field>ID)?"
Not all fields, Banner ID does as its a has_one. This page may help - http://doc.silverstripe.com/doku.php?id=datamodel
"Define 1-to-1 joins in $has_one. The LHS is the relationship name, the RHS is the class you're linking to. This will create a field called (relationship-name)ID, e.g., [[ParentID]]." or in your case BannerID