Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Editing Image fields after upload #71

Open
sanderha opened this issue Mar 3, 2017 · 3 comments
Open

Editing Image fields after upload #71

sanderha opened this issue Mar 3, 2017 · 3 comments

Comments

@sanderha
Copy link

sanderha commented Mar 3, 2017

What would be the best way to give a user the ability to edit the Title field on Image, after it has been uploaded?

I've got a use case where users upload images in a form, and then be able to set a caption for each image. Then the form is submitted.

Not sure if this module out of the box can easily let me build such functionality.

@unclecheese
Copy link
Owner

unclecheese commented Mar 6, 2017

Usually the way you do this is by wrapping the file in a DataObject.

class MyFileDataObject extends DataObject
{
  private static $db = ['Caption' => 'Varchar'];

  private static $has_one = ['File' => 'File'];

  public function getCMSFields()
  {
    $fields = FieldList::create(TabSet::create('Root'));
    $fields->addFieldsToTab('Root.Main', [
      TextField::create('Caption'),
      FileAttachmentField::create('File')
    ]);
    
    return $fields;
}

Then just manage these objects with a GridField.

@sanderha
Copy link
Author

sanderha commented Mar 6, 2017

Indeed, but I'm using it through the frontend .. Currently I use some ajax after upload of the images to load some textfields in for each image.. Just seems pretty crappy.

@satrun77
Copy link

satrun77 commented Mar 22, 2017

I'm working on a project that requires this feature. I have managed to get it working with a bit of work.

  • Set custom preview template that includes placeholder fields for any additional data.
  $upload =  new FileAttachmentField('name');
  $upload->setPreviewTemplate('CustomPreviewTemplate');      
  • Load JS file to access the dropzone isntance.
var uploadInstance = Dropzone.forElement('#id-of-element');
 uploadInstance.on("success", function(file, serverID) {
         // serverID parameter is the database ID of the uploaded file.
         // You can use the serverID to change the name attribute of caption field to something like
         // name="Caption[serverID]['title']" 
});
  • On form submission you can update the File details.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants