I appreciate there are lots of threads about alt and title attributes etc but I haven't found this particular issue addressed.

My use case is quite straightforward: in Media 1.x I want to define alt and title attributes for images on a per-use basis using fields on the content type but when using Media the core fields for these are hidden. The most straightforward way to do this would be with a template.php override

I found an example of this described at http://drupal.stackexchange.com/questions/33758/drupal-7-override-image-... but within hook_preprocess_image the variables for the fields I created aren't available.

Any ideas?


Johann Wagner’s picture

Issue summary:View changes

This worked for me :

template.php of your theme :

 * Implements THEME_preprocess_image().
 * Add metadata to images.
function THEME_preprocess_image(&$vars)
    if (!empty($vars['path']) && ($parsed = parse_url($vars['path']))) {

        $basename         = basename($parsed['path']);
        $dirname          = dirname($parsed['path']);
        $public_string = '/public/';
        $relative_pos     = strpos($parsed['path'], $public_string);
        $relative_path    = substr($dirname, $relative_pos + strlen($public_string));

        if (empty($relative_path))
            $uri = 'public://' . $basename;
            $uri = 'public://' . $relative_path . DIRECTORY_SEPARATOR . $basename;

        $files = file_load_multiple(array(), array('uri' => $uri));
        $file  = reset($files);
        if ($file) {
            $wrapper       = entity_metadata_wrapper('file', $file);
            $vars['alt']   = $wrapper->field_file_image_alt_text->value();
            $vars['title'] = $wrapper->field_file_image_title_text->value();