7

I've added a field to comments to allow users to upload files. I want users to be able to upload different types of files, but if it's an image then show a thumbnail. Can this be done while just using 1 field?

  • Thanks for reputation, this module will be helpful. please make your module available for others. – Mohammad Ali Akbari Sep 27 '12 at 14:37
2
+50

All you need is writing your own display formatter for file fields. have a look at field_example module to see where you should start:

first define your display formatter using hook_field_formatter_info, here with field types you can specify that this display formatter is for file fields.

then all you need is implementing hook_field_formatter_view, here you should output different $element based on $item extension.

after developing this tiny module, go to comment display setting in your content type page and select your new display formatter to rendering that field.

From field example module:

  1. field_example_field_formatter_info
  2. field_example_field_formatter_view
2

I could also create a template for the comment, or the field and then insert an "if".

  • This seems workable, easier for me with my limited programming skills than Alexandru Savin's solution. – Evanss Sep 26 '12 at 13:08
0

I think your best candidate for this job is hook_field_prepare_view(). You can alter field's values just before they get to be displayed. Take a look at the docs here.

  • Im surprised theres no module solution, this seems like a requirement a few people would want. Thanks – Evanss Sep 20 '12 at 13:28
0

I found y'all's answers really helpful, but for my purposes, the custom_formatters module seemed to work really well.

  • How do you use it? It says you can define custom formatters without writing code, but when I went to look at the examples documentation page, it shows only code: drupal.org/node/2532498 – user1359 Apr 26 '16 at 20:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.