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.

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Display count of data objects in template


Go to End


5 Posts   6839 Views

Avatar
DeklinKelly

Community Member, 197 Posts

25 June 2009 at 1:32am

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 %

Avatar
UncleCheese

Forum Moderator, 4102 Posts

25 June 2009 at 2:02am

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

Avatar
DeklinKelly

Community Member, 197 Posts

25 June 2009 at 2:55am

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

Avatar
UncleCheese

Forum Moderator, 4102 Posts

25 June 2009 at 3:20am

Try $TotalItems

Avatar
DeklinKelly

Community Member, 197 Posts

25 June 2009 at 3:27am

$TotalItems works, thanks!