Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

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.

Archive /

Our old forums are still available as a read-only archive.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo

How can I determine if a image is set


Go to End


5 Posts   2514 Views

Avatar
Tkop

28 Posts

21 July 2007 at 10:14am

Edited: 21/07/2007 10:15am

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

Avatar
xmedeko

Community Member, 94 Posts

21 July 2007 at 10:27am

Edited: 21/07/2007 10:29am

[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 %>

Avatar
Willr

Forum Moderator, 5523 Posts

21 July 2007 at 1:27pm

<% if BannerImg %>
    $BannerImg
<% else %>
   <img src=\"default.png\"/>
<% end_if %> 

should work fine btw, we use that method quite alot

Avatar
Tkop

28 Posts

21 July 2007 at 1:50pm

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)?

Avatar
Willr

Forum Moderator, 5523 Posts

22 July 2007 at 2:21pm

"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