Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » How can I determine if a image is set

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

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

Page: 1
Go to End
Author Topic: 1525 Views
  • Tkop
    Avatar
    28 Posts

    How can I determine if a image is set Link to this post

    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

  • xmedeko
    Avatar
    Community Member
    94 Posts

    Re: How can I determine if a image is set Link to this post

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

  • Willr
    Avatar
    Forum Moderator
    5464 Posts

    Re: How can I determine if a image is set Link to this post

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

    should work fine btw, we use that method quite alot

  • Tkop
    Avatar
    28 Posts

    Re: How can I determine if a image is set Link to this post

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

  • Willr
    Avatar
    Forum Moderator
    5464 Posts

    Re: How can I determine if a image is set Link to this post

    "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

    1525 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.