Hi!

I have a problem with the image field (drupal 7 core) and its optional title field: How can the content of the image->title field be shown in a view?

On the image–field – settings I have activated >Enable Title Field<. I use the image->title field to save additional information (what can be seen on the image, what is the source) for each uploaded image.

This part works great. The problem is to show the content of the image->title field in a view. I can find no way to print out the content of a image->title field.

In views 6 & imagefield, views had a special field >field-image-XXX-data< to do this. But with views 7 & image field I can find nothing comparable.

How can this be done with drupal 7?

Using the image->title field seems to be the most efficient way to save additional information for each image if you allow the upload of multiple images in a node...

Comments

Scott J’s picture

I don't know the answer, but adding captions to images is one of the oldest issues for Drupal. This question comes up time and time again, but for some reason, the coders never want to provide an easy option for doing this.

13rac1’s picture

Image captions aren't integrated into Drupal because it is a customization per site. There are at least three ways to add captions to images, in order of complexity(imo):

Scott J’s picture

Thank you eosrei,

The field formatter module posted at http://eosrei.net/articles/2011/03/creating-captioned-image-formatter-fi... is probably the easiest solution for someone (like me), who just wants to turn on captions and get on with posting.

ahillio’s picture

The file_entity module, which is significantly used by the media module, has out-of-the-box "image" types of file entities which have title and alt fields. Either of those could be used for an images "captions".

  • on the file entity:
    • manage display
    • make sure either title or alt text is being displayed
    • probably want to hide the field's label
  • Then on the node type that has the image field:
    • change the image's format to "Rendered file" -- this will display any fields that are shown on the file entities 'manage display' page, so you could add an additional field specifically for caption to the file entity if you want.
arancedisicilia’s picture

Hi there.

My problem was not really views-related, but I came up with a very easy solution that works and can be extended to fit your needs.
My problem was to add a caption to some images, showing the value of their title fields. I altered the default image theming, only for some image styles and not for others. Here is what I added to my custom theme (file template.php).
Please note that this is for D7, and that you have to put your theme's name and image styles name. Or you could try to make it work based on other parameters.

function mytheme_image(&$variables) {

   $attributes = $variables['attributes'];
   $attributes['src'] = file_create_url($variables['path']);

   $apply_theming = false;

   $apply_theming_to_variables = array(
      'style_name' => array(
            'article_picture_preview',
      )
   );
   foreach($apply_theming_to_variables as $var => $val) {
      if(isset($variables[$var]) AND in_array($variables[$var], $val)) {
         $apply_theming = true;
         break;
      }
   }
   if(!$apply_theming) { // Return default theming
      return '<img' . drupal_attributes($attributes) . ' />';
   }

   foreach (array('width', 'height', 'alt', 'title') as $key) {
      if(isset($variables[$key])) {
         $attributes[$key] = $variables[$key];
      }
   }
   // Return custom theming
   return '<img' . drupal_attributes($attributes) . ' /><span class="image-caption">' . (isset($variables['title']) ? $variables['title'] : '') . '</span>';
}

What this does is pretty simple:

  1. Check if the image belongs to a style name called "article_picture_preview" (in my case)
  2. If yes, apply a custom theming, adding a span with the title as caption
  3. If not, return the default image themeing
wooody’s picture

Thats arancedisicilia , its working fine :)

twood’s picture

I was having a problem with colorbox and Image Field Caption module where the caption wasn't showing on the page, only in the colorbox window. I needed to have it display on the page. I had no desire to show a caption off the page but still wanted to give visitors a larger image if they desired. One less module to use. Great job. Thank you.

kaptinfly’s picture

I too am looking for a simple way to add captions via the image field in Drupal 7. I was able to use the media module to accomplish this but I expect to have hundreds of media files and I don't see a good way to create directories to organize the files... just go into the top level of the files directory. Even a straight forward way to add the alt text as a caption using the Genesis theme would work for now. I'm not very good with PHP yet and I'm new to Drupal.

doronin’s picture

You can use the title field contents anywhere in your view with "Rewrite the output of this field". The tokens you are looking for will be in the "Replacement patterns" section. Hope this helps.

kaptinfly’s picture

Could you provide more specific instructions or point to some? Can you create a view that will change the out put for every image on the site?

cbrasfield’s picture

When you add the Content: Image Field to your Views display, you can choose to rewrite the output of the field using 'Rewrite Results'. The tokens exposed are:

[field_image] == Content: Image 
[field_image-fid] == Raw fid
[field_image-alt] == Raw alt
[field_image-title] == Raw title. 

If you want to display the image, use [field_image]. If you want to show a caption from the title field underneath the image, use [field_image]<br />[field_image-title].

Note: there will be an additional descriptor within the fields of use.

mindfl10’s picture

this sounds like a great solution (I've found many solutions in replacement patters in the past). However, the image-title replacement pattern was not working for me. Other replacement patterns such as [field-image] worked fine. Any thoughts on why that pattern would not be working when I am able to enter in titles for the images? I have to say. captions have been quite a challenge for the drupal user w/o php capabilities in D7!

leemako’s picture

Did you get any where with this one? Im having exactly the same issue attempting to display title or alt in a view. The replacement tokens dont seem to work for me either

[EDIT] Almost directly after posting this cracked it, Dont know if it was to do with the update to latest version of views (rc1) or if I had done something different this time and not realised it.

My image field is called 'Image', added two of these to the display and on the second one did a rewrite with the token [field_image-title] (never used to see this but could this time round). Any way it works.

sachbearbeiter’s picture

ha ha drupal and asset management - it's still a fiasco

ambientdrup’s picture

This is not working for me. I'm rewriting the image field and here are my tokens:

[field_gallery_image_1]
[field_gallery_image_1-fid]
[field_gallery_image_1-alt]
[field_gallery_image_1-title]
[field_gallery_image_1-width]
[field_gallery_image_1-height]

I can only get the image to display. The fid, alt, title, width and height do not print/display.

Suggestions?

-Trevor

ambientdrup’s picture

I got this to work for my use-case. More about it here:

http://drupal.org/node/1565908#comment-5966430

Best-

Trevor

shein84’s picture

Great, thx !!!

ñull’s picture

Many thanks for the information! This still works today, but please note that these do not work like the field tokens. The field_image-* tokens are only locally available within the view image field. To my knowledge it is the only way to add attributes to the img tag without also loosing the alt and title. To customise the whole img tag you can do:

<img src="[field_image]" data-big="[field_image_1]" alt="[field_image-alt]" title="[field_image-title]" width="[field_image-width]" height="[field_image-height]" data-link="/node/[nid]" />

In this example I could easily add data-big and data-link, both valid for the View Slideshow Galleria.io plug-in, where [field_image_1] has to come from another image field set to render the larger image URL. Both image fields therefore need to be set to render the Image URL with Multiple field settings to NOT "Display all values in the same row".

rajmataj’s picture

You can access the alt or the title text of the image by doing the following:

  1. Add another field to your View, your node's image field (yes, again)
  2. Ignore the label and display options and go to the fieldset: Multiple Field Settings and uncheck the option: Display all values in the same row
  3. Next, scroll down to the fieldset: Rewrite Results and then click Rewrite the output of this field. Check the Replacement Patterns below and you should see tokens for both alt and title. Use whichever is appropriate.

Works for me!

bailey86’s picture

Bear in mind that the order of the fields might be important. You can sometimes not access a field unless it has been listed higher up.

nikhilbhartiya’s picture

The title field you are talking about can only be seen on hovering the pointer over the image. It is actually a tooltip. In your case completely waste. unmark that checkbox (tiltle and alt).

Nathan Tsai’s picture

Just use Image Field Caption. Enable it and check it in the image settings. (Only for drupal 7 though.)

Pranay Agarwal’s picture

I am using four fields in view. They are:
1. Title
2. Caption
3. Image(Main image)
4. Image(Thumbnails)

Note: This caption field is the field coming up from image_field_caption module I have used.

Third and fourth field are working. Title only appears on hovering the image & the last caption field doesn't appear at all.
I have set the fields in order as mentioned above. Also, in gallerific fields, I have set all the four fields.

(Tried using replacement patterns, excluding field, hiding from manage display, followed documentation but failed.)

I want to display this caption field underneath the main image. Any leads would be highly appreciated.

SandraStauffer’s picture

Best of luck