Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » Many images

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

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

Page: 1 2 3
Go to End
Author Topic: 9400 Views
  • jam13
    Avatar
    121 Posts

    Many images Link to this post

    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.

  • UncleCheese
    Avatar
    4085 Posts

    Re: Many images Link to this post

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

  • cinek
    Avatar
    Community Member
    17 Posts

    Re: Many images Link to this post

    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.

  • Aaron
    Avatar
    Community Member
    63 Posts

    Re: Many images Link to this post

    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

  • cinek
    Avatar
    Community Member
    17 Posts

    Re: Many images Link to this post

    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.

  • Aaron
    Avatar
    Community Member
    63 Posts

    Re: Many images Link to this post

    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

  • UncleCheese
    Avatar
    4085 Posts

    Re: Many images Link to this post

    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!

  • Aaron
    Avatar
    Community Member
    63 Posts

    Re: Many images Link to this post

    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.

    9400 Views
Page: 1 2 3
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.