Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » Image attach to content item not working

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

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

Page: 1 2
Go to End
Author Topic: 4717 Views
  • Decisive Flow
    Avatar
    Community Member
    73 Posts

    Image attach to content item not working Link to this post

    I have added in the abiulity to upload an image for a certain content item, however when I select "fromt he file store" then select the image I want to attach, it appears to upload, but nothing else happens (the upload boxes don't disappear)
    This is what the code says: $fields->addFieldToTab("Root.Content.Main", new ImageField("LogoID", "Logo"), "Content");

    Therefore, when I add $Logo into the associated .ss file, I assume the image should show up in the page... but it doesnt.

    All the other additions work great ($URL, $MainContact) so I'm not entirely sure how to fix this....

    Thanks for any help and sorry about the massive description

  • Simon
    Avatar
    Core Development Team
    27 Posts

    Re: Image attach to content item not working Link to this post

    To get an image attached to a item, you need to create the relationship between your object (Page.php ? ) and and the image have to add the following code

    // This creates the relationship for the image.
    static $has_one = array(
       "Image" => "Image",
    );

    // this adds the image to the CMS.
    function getCMSFields() {
          $fields = parent::getCMSFields();
       $fields->addFieldToTab("Root.Content.Images", new ImageField("Image", "Image"));
          return $fields;
    }

    Note: You'll have to run a DB build after adding the image code to your class (e.g Page.php) You'll then be able to save the image against the page. Now we just have to add the code to the template, (page.ss)

    You can use a control to change the properties of the image on the fly,

    eg, adding

    <% control Image.SetWidth(80) %>
    <img src="$URL" width="80" alt="image alt text" />
    <% end_control %>

    to your template would resize automatically the image to a set width of 80px.

    If your still having problems check to ensure the same reference to Image is in both the relationship, it can upload and save to the CMS, and finally that your on the right page (it can happen).

    See if that helps,
    Simon

  • Decisive Flow
    Avatar
    Community Member
    73 Posts

    Re: Image attach to content item not working Link to this post

    actually....

    I have had the help of sam on this, he set it up with all that stuff:

    This is the code:

    -------------------------------------------------------------------------

    class CoolCompany extends BlogHolder {
       static $db = array(
          "URL" => "Varchar",
          "MainContact" => "Varchar",
          "MainEmail" => "Varchar",
          "Address" => "Text",   
       );
       static $has_one = array(
          "Logo" => "Image",
       );
       
       function getCMSFields() {
          $fields = parent::getCMSFields();
          
          $fields->addFieldToTab("Root.Content.Main", new ImageField("LogoID", "Logo"), "Content");
          $fields->addFieldToTab("Root.Content.Main", new TextField("URL"), "Content");
          $fields->addFieldToTab("Root.Content.Main", new TextField("MainContact", "Main Contact"), "Content");
          $fields->addFieldToTab("Root.Content.Main", new TextField("MainEmail", "Main Email"), "Content");
          $fields->addFieldToTab("Root.Content.Main", new TextareaField("Address", "Address", 3), "Content");
          
          return $fields;
       }   
    }

    class CoolCompany_Controller extends BlogHolder_Controller {

    }

    ?>

    ------------------------------------------------------

    Ive flushed the database many a time, all the rest of the fields are coming through just fine, and I've tried that <? control > thing in the template file, but no go...

    I am wondering if the upload isnt working?

    FYI. As Sam will agree, these issues are normally a result of my incompetance and nothing to do with Silverstripe, but I'm really stuck ont his one....

    Thanks heaps for your help!

  • Sam
    Avatar
    Administrator
    679 Posts

    Re: Image attach to content item not working Link to this post

    I think that you want to put the Logo field on CoolCompanyIndividual instead of CoolCompany.

    From the looks for things, CoolCompany is set up to be a "holder" page, and CoolCompanyIndividual represents each company. It's the companies that should have a logo, not the "holder page".

  • Decisive Flow
    Avatar
    Community Member
    73 Posts

    Re: Image attach to content item not working Link to this post

    The problem appears to be that images arent getting uploaded/attached as they should be... i.e. When I click upload, the input boxc isn't replaced witht he picture.

    The code seems to be ok, pointing to the /assets/ folder, but no further...?

  • sonicparke
    Avatar
    74 Posts

    Re: Image attach to content item not working Link to this post

    Did anyone ever get this working? I need to do this as well. It gives me a path assets but nothing after that. It is uploading the file though.

  • nodevice
    Avatar
    Community Member
    37 Posts

    Re: Image attach to content item not working Link to this post

    It worked in my site until updated to SilverStripe 2.2.0-rc1. From this point it only works when I upload a file but not when attaching from the file storage.

  • Fuzz10
    Avatar
    Community Member
    787 Posts

    Re: Image attach to content item not working Link to this post

    The image attach thingy is still broken in 2.2

    This is the result I get in Internet Explorer :

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