You are here

image_formatter_link_to_image_style.module in Image formatter link to image style 7

Same filename and directory in other branches
  1. 8 image_formatter_link_to_image_style.module

Provides an additional formatter for image core field, to link to an image style.

File

image_formatter_link_to_image_style.module
View source
<?php

/**
 * @file
 * Provides an additional formatter for image core field,
 * to link to an image style.
 */

/**
 * Implements hook_field_formatter_info().
 */
function image_formatter_link_to_image_style_field_formatter_info() {
  $formatters = array(
    'image_formatter_link_to_image_style' => array(
      'label' => t('Image link to image style'),
      'field types' => array(
        'image',
      ),
      'settings' => array(
        'image_style' => '',
        'image_link_style' => '',
        'link_class' => '',
        'link_rel' => '',
        'image_class' => '',
      ),
    ),
  );
  return $formatters;
}

/**
 * Implements hook_field_formatter_settings_form().
 */
function image_formatter_link_to_image_style_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
  $display = $instance['display'][$view_mode];
  $settings = $display['settings'];
  $image_styles = image_style_options(FALSE);
  $element['image_style'] = array(
    '#title' => t('Image style'),
    '#type' => 'select',
    '#default_value' => $settings['image_style'],
    '#empty_option' => t('None (original image)'),
    '#options' => $image_styles,
  );
  $element['image_link_style'] = array(
    '#title' => t('Link image to image style'),
    '#type' => 'select',
    '#default_value' => $settings['image_link_style'],
    '#empty_option' => t('None (original image)'),
    '#options' => $image_styles,
  );
  $element['link_class'] = array(
    '#title' => t('Class(es) to add to the link'),
    '#type' => 'textfield',
    '#default_value' => $settings['link_class'],
  );
  $element['link_rel'] = array(
    '#title' => t('Rel attribute value to add to the link'),
    '#type' => 'textfield',
    '#default_value' => $settings['link_rel'],
  );
  $element['image_class'] = array(
    '#title' => t('Class(es) to add to the image element'),
    '#type' => 'textfield',
    '#default_value' => $settings['image_class'],
  );
  return $element;
}

/**
 * Implements hook_field_formatter_settings_summary().
 */
function image_formatter_link_to_image_style_field_formatter_settings_summary($field, $instance, $view_mode) {
  $display = $instance['display'][$view_mode];
  $settings = $display['settings'];
  $summary = array();
  $image_styles = image_style_options(FALSE);

  // Unset possible 'No defined styles' option.
  unset($image_styles['']);

  // Styles could be lost because of enabled/disabled modules that defines
  // their styles in code.
  if (isset($image_styles[$settings['image_style']])) {
    $summary[] = t('Image style: @style', array(
      '@style' => $image_styles[$settings['image_style']],
    ));
  }
  else {
    $summary[] = t('Original image');
  }
  if (isset($image_styles[$settings['image_link_style']])) {
    $summary[] = t('Linked to image style: @style', array(
      '@style' => $image_styles[$settings['image_link_style']],
    ));
  }
  else {
    $summary[] = t('Linked to original image');
  }
  if (!empty($settings['link_class'])) {
    $summary[] = t('Link class(es): @classes', array(
      '@classes' => $settings['link_class'],
    ));
  }
  if (!empty($settings['link_rel'])) {
    $summary[] = t('Link rel attribute: @rel', array(
      '@rel' => $settings['link_rel'],
    ));
  }
  if (!empty($settings['image_class'])) {
    $summary[] = t('Image element class(es): @classes', array(
      '@classes' => $settings['image_class'],
    ));
  }
  return implode('<br />', $summary);
}

/**
 * Implements hook_field_formatter_view().
 */
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;
}