1

Out of the box, image file entities have alt and title text fields attached, field_file_image_alt_text and field_file_image_title_text respectively, that provide the rendered image with an HTML alt and title text.

Today I feel like Mysite.com would be a great title for the images on my website, and I could easily programatically populate both text fields with Mysite.com upon upload of the image file. However, what if tommorrow I change my mind and instead of Mysite.com I want the alt and title text to be Welcome to Mysite.com? I would have to edit all file entities and modify both text fields for each.

Is there a way to render the alt and title text through a theming function so that I do not have to actually store the alt and title text within each file entity? I want to be able to modify the alt and title texts of all images by just modifying a single line of code.

1

The logic is pretty easy, you can just preprocess theme_image():

function MYTHEME_preprocess_image(&$vars) {
  $vars['attributes']['alt'] = 'alt';
  $vars['attributes']['title'] = 'title';
}

If you're using any of the standard image display formatters they'll eventually be passed through theme_image() so that should work for all fields.

I'd strongly advise you consider not doing this though. The point of an alt tag is to describe the image to parties who can't interpret it in its original form (visually impaired people, robots, text only browsers, search engines, etc)

By setting the alt for every image to the same string, you're making your site quite inaccessible to those groups of visitors. And since Google and others assign a reasonable weight to alt text, you won't be doing your SEO any favours either.

  • I was simplifying my question. I want to load a submitted title (from a form field) and the author's username to build a custom alt and title text. – deinqwertz Aug 13 '13 at 14:18
  • Ah ok that makes more sense, the above method along with the logic to combine the username with the saved var should do the trick – Clive Aug 13 '13 at 14:19
  • I will test it immediately, thank you very much for your help! – deinqwertz Aug 13 '13 at 14:21
  • Unfortunately, the alt and title text doesn't change when I add the function to my template.php. Is it possible that file entities' built-in alt and title functionality overrides the preprocess function? – deinqwertz Aug 16 '13 at 20:02
  • Do you have any idea how to get around this issue? – deinqwertz Aug 18 '13 at 21:29

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.