Jump to:

7938 Posts in 1541 Topics by 945 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » Display count of data objects in template

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Page: 1
Go to End
Author Topic: 4136 Views
  • DeklinKelly
    Avatar
    Community Member
    197 Posts

    Display count of data objects in template Link to this post

    I use the DataObjectManager module to enable the admin to upload multiple images. My .ss template then displays each image.

    I want to display a count if images with each image, like this:

    <img scr="image.jpg" alt="image" />Image 1 of 3
    <img scr="image.jpg" alt="image" />Image 2 of 3
    <img scr="image.jpg" alt="image" />Image 3 of 3

    <?php
    class CarouselImage extends DataObject {
    static $db = array (
    'Caption' => 'Varchar(200)'
    );

    static $has_one = array (
    'Image'=>'Image',
    'Parent'=>'SiteTree'
    );

    /**
    * Gets the fields for the cms
    * @return {FieldSet} Field set for use in the popup
    */
    public function getCMSFields_forPopup() {
    $fileField=new FileIFrameField('Image');
    $fileField->setAllowedExtensions(array('jpg','gif','png'));

    $fields=new FieldSet(
    $fileField,
    new TextField('Caption','Caption',$this->Caption,200),
    new HiddenField('ParentIDCache','ParentIDCache',$this->getParentIndex())
    );

    return $fields;
    }

    /**
    * Forces the parent id before calling the parent's onBeforeWrite
    */
    public function onBeforeWrite() {
    if(!isset($this->record['ParentIDCache'])) {
    $pId=array_values($this->toMap());

    $this->record['ParentID']=$pId[2];
    }else {
    $this->ParentID=$this->record['ParentIDCache'];
    }

    parent::onBeforeWrite();
    }

    /**
    * Gets the parent index of the data object
    * @return {int} Page's internal ID
    */
    private function getParentIndex() {
    $pId=array_values($this->toMap());
    if(isset($this->ParentID) && $this->ParentID!=null && $this->ParentID!=0) {
    return $this->ParentID;
    }else {
    return $pId[1];
    }
    }
    }
    ?>


    Here is code from my .ss template

    <% control CarouselImages %>
    <img src="$Image.URL" alt="$Caption.ATT" />
    <% end_control %

  • UncleCheese
    Avatar
    4085 Posts

    Re: Display count of data objects in template Link to this post

    <% control CarouselImages %>
    <img src="$Image.URL" alt="$Caption.ATT" /> Image $Pos of $Count
    <% end_control %

  • DeklinKelly
    Avatar
    Community Member
    197 Posts

    Re: Display count of data objects in template Link to this post

    Thanks, $Pos works but $Count does not. What should I do to make $Count work?

  • UncleCheese
    Avatar
    4085 Posts

    Re: Display count of data objects in template Link to this post

    Try $TotalItems

  • DeklinKelly
    Avatar
    Community Member
    197 Posts

    Re: Display count of data objects in template Link to this post

    $TotalItems works, thanks!

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