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.

Archive /

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

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

Many images


Go to End


23 Posts   11408 Views

Avatar
jam13

121 Posts

1 November 2007 at 1:04am

Has anyone come up with a good way of dealing with the problem of associating multiple images with a page?

I've tried most of the TableField derivatives, but none of these work very well with ImageFields.

What I'm thinking of is something like this:

1) New class "MyImages" which extends Images by adding extra metadata to an image.

2) New class "MyPage" which extends SiteTree, and has_many MyImages

3) A control set of some kind which is added to the CMS for MyPages that allows multiple images to be associated with the page (either by selecting from existing uploads or allowing new images to be uploaded).

1 and 2 are no problem, but I'm stumped on 3.

Avatar
UncleCheese

Forum Moderator, 4102 Posts

31 May 2008 at 6:24am

Have you gotten anywhere with this? I'm looking for the same solution.

Avatar
cinek

Community Member, 17 Posts

4 June 2008 at 9:14am

Edited: 04/06/2008 9:15am

It's quite easy to achieve with ComplexTableField.

I have an ImageAttachment class that has a name, image and a thumbnail image:

class ImageAttachment extends DataObject {
        static $db = array(
                'Name' => 'Text'
        );
        static $has_one = array(
                'ImageThumbnail' => 'Image',
                'Image' => 'Image'
        );

        static $field_names = array('Name' => 'Name');

        function getCMSFields_forPopup() {
                $fields = new FieldSet();
                $fields->push(new TextField('Name', 'Name'));
                $fields->push(new ImageField('ImageThumbnail', 'Thumbnail'));
                $fields->push(new ImageField('Image', 'Image')); 

                return $fields;
        }
}

In the page I add a new tab with ComplexTableField that allows me edit all the related images:

class PageWithImages extends Page {
        static $has_many = array(
                'ImageAttachments' => 'ImageAttachment'
        );

        function getCMSFields() {
                $fields = parent::getCMSFields();

                $imagetable = new ComplexTableField(
                        $this,
                        'ImageAttachments',    // relation name
                        'ImageAttachment',     // object class
                        ImageAttachment::$field_names,   // fields to show in table
                        ImageAttachment::getCMSFields_forPopup(),  // form that pops up for edit
                        "PageID = {$this->ID}",  // a filter to only display item associated with this page
                        "Name ASC"      // Sort by name
                );

                $fields->addFieldToTab('Root.Content.ImageAttachments', $imagetable);

                return $fields;
        }
}

I use this successfully for file attachments as well.
You may also use HasManyComplexTableField if you have a set of images and want to associate a set of images with each page but possibly one image to multiple pages. With ComplexTableField as above each ImageAttachment may be added to one page only.

Avatar
Aaron

Community Member, 63 Posts

7 June 2008 at 5:09pm

Hi cinek - this is exactly what I am after. I have a custom page type that we want to add an easy interface so we can attach multiple images to each page.

I attempted to patch your code into my page class. DB flush looks all good, however, I get the "There has been an error" javascript box when I try to create or edit this page type.

In firebug the error is: http://mysite.localhost/admin/getitem?ID=35&ajax=1 (500)

I'm using 2.2.2, have you had to adjust your code for the update at all. I can't find anything serious for ComplexTableField.

I have also tried making a page type from scratch using your code as posted by itself. Same problem.

Any Ideas?
Cheers

Avatar
cinek

Community Member, 17 Posts

7 June 2008 at 8:02pm

The code in my post is not the code I run :) I have simplified it so that it is clearer, there may be some typos or silly mistakes (mainly in names of relationships and/or classes).

Unfortunately now I don't have time to help you further, but I will either post my original code or try to find a problem with this example on monday evening (European time).
Unless you find the problem sooner :)

If you have time, please read the documentatin for ComplexTableField __construct and check whether all the arguments make sense.

You may also try to test this in a clean project to make sure that there is no left-over from your earlier trials (silverstripe never removes columns for old page types from database).

As to the upgrade - I started with 2.2.1 and didn't have to change this code for 2.2.2.

Avatar
Aaron

Community Member, 63 Posts

8 June 2008 at 11:41am

No Problem, that's what I was doing today anyway. I found another thread that had a working example, however it too is not perfect.
I'm really keen to get this working - if so I will post the code as it seems to be a fairly popular request.
Cheers
Aaron

Avatar
UncleCheese

Forum Moderator, 4102 Posts

10 June 2008 at 2:51am

The problem with this approach, at least last time I checked, is that you need to save the record before you upload the image. Is that still true? That's kind of annoying if you only have a file upload field in your popup and no TextFields (i.e. "name"). There's no way to save the record!

Avatar
Aaron

Community Member, 63 Posts

10 June 2008 at 9:51am

Yep, I know what you mean.
I've ended up adding ten image fields instead. I don't think SS is ready for this sort of thing yet.

Go to Top