Jump to:

23377 Posts in 18296 Topics by 2867 members

General Questions

SilverStripe Forums » General Questions » SOLVED: Calling the first item in a many_many array

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Page: 1
Go to End
Author Topic: 633 Views
  • LinseyM
    Avatar
    Community Member
    99 Posts

    SOLVED: Calling the first item in a many_many array Link to this post

    Hi,

    I've customised the CMS so that the user can chosse multiple uploaded images to display in an image scroller using a treemultiselect field.

    Done this as follows:

    public static $many_many = array(
    "ChooseImages" => "File"
    );

    function getCMSFields() {
    $fields = parent::getCMSFields();
    $fields->addFieldToTab('Root.Content.PhotoGallery', new TreeMultiSelectField('ChooseImages', 'Select images for gallery:', 'File'), '');
    return $fields;
    }

    this is then called in the template as:

    <% control ChooseImages %><img src="$Link" border="0" height="110" width="167" ><% end_control %>

    This works perfectly, and loads up every image that the user selected.

    However if I only want to call the first item of the array (eg only access the first image that is selected) how do I do that? Assuming its a function, tried a few things but no success... eg in the page controller

    class CoursePage_Controller extends Page_Controller {
       
       function GetFirstPic() {
          $a = ChooseImages[0];
          return ($a);
       }
    }

    Didn't really expect it to work, but worth a try! ;-)

  • simon_w
    Avatar
    Forum Moderator
    471 Posts

    Re: SOLVED: Calling the first item in a many_many array Link to this post

    You could either use:

    return $this-> ChooseImages()->First();

    or, do it straight in your template with:

    <% control ChooseImages.First %>$SetSize(167, 110)<% end_control %>

  • LinseyM
    Avatar
    Community Member
    99 Posts

    Re: SOLVED: Calling the first item in a many_many array Link to this post

    Well, that was simple. Works perfectly. Thank you!

    633 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.