You are here

function image_formatter_link_to_image_style_field_formatter_view in Image formatter link to image style 7

Implements hook_field_formatter_view().

File

./image_formatter_link_to_image_style.module, line 117
Provides an additional formatter for image core field, to link to an image style.

Code

function image_formatter_link_to_image_style_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();
  foreach ($items as $delta => $item) {
    if (!empty($display['settings']['image_link_style'])) {
      $path = image_style_url($display['settings']['image_link_style'], $item['uri']);
    }
    else {
      $path = file_create_url($item['uri']);
    }
    $uri = array(
      'path' => $path,
      'options' => array(),
    );

    // Add in the link classes.
    if (!empty($display['settings']['link_class'])) {
      $a_classes = explode(' ', check_plain($display['settings']['link_class']));
      if (!empty($uri['options']['attributes']['class'])) {
        if (is_array($uri['options']['attributes']['class'])) {
          $uri['options']['attributes']['class'] = array_merge($uri['options']['attributes']['class'], $a_classes);
        }
        elseif (is_string($uri['options']['attributes']['class'])) {
          $uri['options']['attributes']['class'] .= " {$a_classes}";
        }
      }
      else {
        $uri['options']['attributes']['class'] = $a_classes;
      }
    }

    // Add the rel.
    if (!empty($display['settings']['link_rel'])) {
      $uri['options']['attributes']['rel'] = check_plain($display['settings']['link_rel']);
    }

    // Add the image classes.
    if (!empty($display['settings']['image_class'])) {
      $img_classes = explode(' ', check_plain($display['settings']['image_class']));
      if (!empty($item['attributes']['class'])) {
        if (is_array($item['attributes']['class'])) {
          $item['attributes']['class'] = array_merge($item['attributes']['class'], $img_classes);
        }
        elseif (is_string($item['attributes']['class'])) {
          $item['attributes']['class'] .= " {$img_classes}";
        }
      }
      else {
        $item['attributes']['class'] = $img_classes;
      }
    }
    $element[$delta] = array(
      '#theme' => 'image_formatter',
      '#item' => $item,
      '#image_style' => $display['settings']['image_style'],
      '#path' => $uri,
    );
  }
  return $element;
}