1

I'm looking for a way to add a class field to Drupal's image field (field_image). At the moment my images are displayed using code like this:

<img alt="Alternate text here" src="/path/to/image" />

I'd like to add a class field so that the code would be like so:

<img alt="Alternate text here" class="portrait floatLeft" src="/path/to/image" />

I think I could achieve this by adding a field to the content type to store the class name - I could then use views to add the class value to the image's parent DIV. However, I suspect there's a way to add a class field to the image field itself - it's not uncommon for images to have classes ;)

I've tried the File Entity module, which according to the documentation allows you to create custom file types. Sadly it doesn't explain how.

Any pointers / suggestions would be much appreciated.

Edit: To clarify, I'd like to be able to add classes on a per image basis. Some images might need the classes portrait floatLeft, others might need landscape floatNone and so forth.

1

Consider looking at the Image Class module, which extends the Image formatter by adding an option to add an element class.

It requires the Field formatter settings module (dependency).

  • Thanks, I had looked into that module. It allows you to add the same class to all images (defined on the Manage display tab), which is not what I'm after. I want to be able to define a class when adding an image (they won't all have the same class). – rkhff Jun 18 '15 at 17:35
  • @rkhff I don't believe classes will follow an image, so you would have to determine the class when it was output, not when it was added, or create a custom module to track images and the optional classes they posses – Geoff Jun 18 '15 at 18:39

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.