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.

Data Model Questions

List images from upload directory


Go to End
Reply

12 Posts   4667 Views

Avatar
Teere

16 June 2010 at 3:24am Community Member, 1 Post

Hey!

As the topic stated, im trying to list image files from a certain folder in the upload directory (Uploaded with the built in fileuploader).
Since I wish to display these on the front page.
Being quite new to silverstripe and I cant really find any direct information on this.

Example:

image1.jpg in folder /myfrontpageimages

to

<img src="thesource/$src" alt="$alt" title="$title" />

Avatar
Hamish

17 June 2010 at 10:04am Community Member, 712 Posts

Hi there, welcome to the community.

Files and Folders in your assets folder can be used like other database objects. You can pick a specific folder with:

$folder = DataObject::get_one("Folder", "Filename = 'assets/myfrontpageimages'");

From here it is probably easiest to get all images that have a parent ID equal to this Folders ID:

$images = DataObject::get("Image", "ParentID = '{$folder->ID}'");

The above code can be wrapped into a method in you Page class (with some additional checking that the folder exists):

function getFrontPageImages() {
$folder = DataObject::get_one("Folder", "Filename = 'assets/myfrontpageimages'");
return $folder ? DataObject::get("Image", "ParentID = '{$folder->ID}'") : false;
}

Now, from your template you can do:

<% if FrontPageImages %>
<% control FrontPageImages %>
$Tag
<% end_control %>
<% end_if %>

(FYI $Tag calls the Image method getTag() which propduces standard <img src=".." alt=".."> HTML for your template)

Hope this helps.

Avatar
spierala

18 July 2012 at 8:33pm Community Member, 80 Posts

Hello,
thanks that works still in SS3.

I wonder how can I make a has_one relationship between Page and Folder, so the user can set the "Images Folder" for a certain page in the backend.

Kind regards,
Florian

Avatar
lerni

19 July 2012 at 10:21am Community Member, 80 Posts

hi florian

What have you done so far? Anyway Folder extends File. You need a has_one to a Folder and TreeDropdownField could be a Field to select this.

lukas

Avatar
spierala

21 July 2012 at 9:39am Community Member, 80 Posts

Hey Lukas!
that worked perfectly! was quite easy that way...

My final code looked like that:

$fields->addFieldToTab('Root.Main', new TreeDropdownField('FolderID', 'Choose Image Folder', 'Folder'), 'Content');

FolderID is automatically generated for the CustomPage Table when putting this code in CustomPage.php:

static $has_one = array (
'Folder' => 'Folder'
);

thx,
Florian

Avatar
acktivate

14 April 2013 at 4:34am (Last edited: 14 April 2013 4:57am), Community Member, 11 Posts

I am running SS3 and dropped this code onto my page.php page and inserted the the loop control on the ss page and I can't get it working. I've turned on debugging and the Filename that I am setting does not seem to get referenced.

page.php
function RotateImages() {
      $folder = DataObject::get_one("Folder", "Filename = 'assets/myfrontpageimages'");
      Debug::show(DataObject::get_one("Folder"));
      Debug::show(DataObject::get_one("Image"));
      return $folder ? DataObject::get("Image", "ParentID = '{$folder->ID}'") : false;
      
      
}
Debug Folder returns Filename: assets/Uploads/2011-Totally-Awesome-Awards/
Debug Image returns Filename: assets/Themes/Ahoy/ahoy.jpg

somepage.ss
<% if RotateImages %>
         <% loop RotateImages %>
$Tag
         <% end_loop %>
      <% end_if %>
I am using a rotating image slider on the website and using this function to write the current list of image files.

Avatar
spierala

14 April 2013 at 5:28am Community Member, 80 Posts

Hi!
what is displayed if you put $$Debug instead of $Tag in the template?
could you maybe also debug this:
Debug::show(DataObject::get("Image", "ParentID = '{$folder->ID}'"));

and are the Image Entries for sure created in the Database (in the File table)?
maybe not if you just uploaded the images without running /dev/tasks/FilesystemSyncTask

Florian

Avatar
acktivate

14 April 2013 at 6:35am Community Member, 11 Posts

Hi - thank your for your reply.

I added $$Debug and received this:

Name:RotateImages
Table:
Value:

I also ran /dev/tasks/FilesystemSyncTask and it added the 5 files I had uploaded to the directory.

When I add Debug::show(DataObject::get("Image", "ParentID = '{$folder->ID}'")); I receive an error.

Go to Top