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.

Archive /

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

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

Image attach to content item not working


Go to End
Reply


9 Posts   4791 Views

Avatar
Decisive Flow

Community Member, 73 Posts

9 November 2006 at 3:57pm

Edited: 09/11/2006 3:57pm

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

Avatar
Simon

Core Development Team, 27 Posts

10 November 2006 at 10:04am

Edited: 10/11/2006 10:13am

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

Avatar
Decisive Flow

Community Member, 73 Posts

10 November 2006 at 11:58am

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!

Avatar
Sam

Administrator, 685 Posts

10 November 2006 at 2:59pm

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".

Avatar
Decisive Flow

Community Member, 73 Posts

13 November 2006 at 8:51am

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...?

Avatar
sonicparke

74 Posts

14 November 2007 at 12:52pm

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.

Avatar
nodevice

Community Member, 37 Posts

17 November 2007 at 2:07am

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.

Avatar
Fuzz10

Community Member, 787 Posts

6 December 2007 at 12:34am

The image attach thingy is still broken in 2.2

This is the result I get in Internet Explorer :

Go to Top