How do I tweak an image field markup? I have an image field that is set to link to content. Drupal makes this markup:

&lt;a href="/content-page"&gt;
  &lt;img src="http://mysite/sites/default/files/image.jpg"&gt;
&lt;/a&gt;


Now I need to tweak the markup and add some styling, for example:

&lt;a href="/content-page" style="border-style: none !important;"&gt;
  &lt;img src="http://mysite/sites/default/files/image.jpg" style="border: none; outline: none;"&gt;
&lt;/a&gt;


I want to add the inline CSS in this way because the page is for emailing.

What would be the best way to do this? I have tried:


Putting the markup into field--myfield.tpl.php and inserting variables, but the $items array doesn't give me clean image file or target URLs.
Using the Custom Formatters module, but it gives me AJAX errors for some reason and not sure it would do the job anyway.
I could use a preprocess function in template.php, but I am hot sure how.
For link, it's easy to provide in field--myfield.tpl.php:

$items[0]['#path']['options']['attributes'] = array('style'=&gt;'border-style: none !important;');


But image doesn't have this, so you can miss upper code and rewrite formatter in template.php (add attributes there).

/**
 * Returns HTML for an image field formatter.
 *
 * @param $variables
 *   An associative array containing:
 *   - item: An array of image data.
 *   - image_style: An optional image style.
 *   - path: An array containing the link 'path' and link 'options'.
 *
 * @ingroup themeable
 */
function YOURTHEMENAME_image_formatter($variables) {
  $item = $variables['item'];
  $image = array(
    'path' =&gt; $item['uri'],
    'alt' =&gt; $item['alt'],
  );
  // Do not output an empty 'title' attribute.
  if (drupal_strlen($item['title']) &gt; 0) {
    $image['title'] = $item['title'];
  }

  if ($variables['image_style']) {
    $image['style_name'] = $variables['image_style'];
    $output = theme('image_style', $image); // Here you should add attribute, check that it's your field.
  }
  else {
    $output = theme('image', $image);
  }

  if (!empty($variables['path']['path'])) {
    $path = $variables['path']['path'];
    $options = $variables['path']['options'];
    // When displaying an image inside a link, the html option must be TRUE.
    $options['html'] = TRUE;
    $output = l($output, $path, $options);  // Here you should add attribute, check that it's your field. Or just take upper code.
  }

  return $output;
}