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

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.

Scott Jackson
Wollongong NSW Australia

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):

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.

Scott Jackson
Wollongong NSW Australia

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

Thats arancedisicilia , its working fine :)

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.

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.

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?

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.

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!

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.

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

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

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

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

Best-

Trevor

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!

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.

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

niks