From 154645aa6bfe3d7a9931d43e1208bd92eafa2b3e Mon Sep 17 00:00:00 2001
From: interdruper <interdruper@2437374.no-reply.drupal.org>
Date: Fri, 14 Mar 2014 16:29:36 +0100
Subject: [PATCH] D7 imagecache external function support

---
 link_image.install |   2 +-
 link_image.module  | 106 ++++++++++++++++++++++++++++++++++++++++++++++++++---
 2 files changed, 101 insertions(+), 7 deletions(-)

diff --git a/link_image.install b/link_image.install
index 7e91782..5a3b80f 100644
--- a/link_image.install
+++ b/link_image.install
@@ -8,5 +8,5 @@
  * Implements hook_uninstall().
  */
 function link_image_uninstall() {
-  db_delete('variable')->condition('name', 'link_image_%%', 'LIKE');
+  db_query("DELETE FROM {variable} WHERE name LIKE `link_image_%%`");
 }
diff --git a/link_image.module b/link_image.module
index ebc0cf7..e02a943 100644
--- a/link_image.module
+++ b/link_image.module
@@ -1,8 +1,8 @@
 <?php
 /*
  * @file link_image.module
- * Defines a field formatter to display a link to an image as an HTML image
- * element.
+ * Defines a field formatter to display a link to an external image as an HTML image
+ * element. It uses imagecache_external to build local cache image style files.
  */
 
 /**
@@ -11,19 +11,88 @@
 function link_image_field_formatter_info() {
   return array(
     'link_image' => array(
-      'label' => t('Image with link as source URL'),
+      'label' => t('Image with URL as source, title as alt tags.'),
       'field types' => array('link_field'),
+      'settings'  => array(
+        'img_style'  => 'thumbnail',
+        'show_title' => ''
+      ),
     ),
   );
 }
 
+/**
+ * Implements hook_field_formatter_settings_form().
+ */
+function link_image_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
+  $display = $instance['display'][$view_mode];  //This gets the view_mode where our settings are stored
+  $settings = $display['settings'];  //This gets the actual settings
+  $element = array();  //Initialize the element variable
+
+  // Image style select box only if Imagecache External module is enabled
+  if(module_exists('imagecache_external')) {
+    $image_styles = image_style_options(FALSE);
+    $element['img_style'] = array(
+      '#type'           => 'select',                           // Use a select box widget
+      '#title'          => t('Image style'),                   // Widget label
+      '#description'    => t('Select which size for the image'), // Helper text
+      '#empty_option'   => t('None (original image)'),
+      '#default_value'  => $settings['img_style'],              // Get the value if it's already been set
+      '#options'        => $image_styles,
+    );
+  }
+
+  // Show title as caption.
+  $element['show_title'] = array(
+    '#title' => t('Show Title'),
+    '#description' => t('Show title as caption.'),
+    '#type' => 'checkbox',
+    '#default_value' => isset($settings['show_title']) ? $settings['show_title'] : FALSE,
+  );
+
+  return $element;
+}
+
+/**
+ * Implements hook_field_formatter_settings_summary().
+ */
+function link_image_field_formatter_settings_summary($field, $instance, $view_mode) {
+  $display = $instance['display'][$view_mode];
+  $settings = $display['settings'];
+
+  $image_styles = image_style_options(FALSE, PASS_THROUGH);
+  // 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 (module_exists('imagecache_external') && isset($image_styles[$settings['img_style']])) {
+    $summary = t('Display URL as an image, using the @size image style.<br>Show Title: @show_title', array(
+      '@size'       => $settings['img_style'],
+      '@show_title' => $settings['show_title'] ? t('Yes') : t('No'),
+    ));
+  }
+  else {
+    $summary = t('Display URL as an image, using its original size.<br>Show Title: @show_title', array(
+      '@show_title' => $settings['show_title'] ? t('Yes') : t('No'),
+    ));
+  }
+
+  return $summary;
+}
+
 
 /**
  * Implements hook_field_formatter_view().
  */
 function link_image_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
+  $settings = $display['settings'];         // get the settings
+  $style = $settings['img_style'];          // The style setting selected in the settings form
+  $show_caption = $settings['show_title'];  // Show title as caption
+
   $element = array();
   foreach ($items as $delta => $item) {
+    $item['style_name'] = $style; // embed this  setting in the item
+    $item['show_caption'] = $show_caption;
     $element[$delta] = array(
       '#theme' => 'link_image_formatter',
       '#element' => $item,
@@ -55,16 +124,41 @@ function link_image_theme() {
 function theme_link_image_formatter($vars) {
   $item = $vars['element'];
 
+  // add the query part back to the url, provided there is any
+  if(isset($item['query'])) {
+    $query = "?";
+    foreach ($item['query'] as $key => $value) {
+      $query = $query . $key . "=" . $value . "&";
+    }
+    $fullurl = $item['url'] . $query;
+    } else {
+      $fullurl = $item['url'];
+  }
+
   $image = array(
-    'path' => theme('link_formatter_link_plain', $vars),
+    'path' => $fullurl,
+    'style_name'=> $item['style_name'],
   );
+
   // Do not output an empty 'title' attribute.
-  if (drupal_strlen($item['title']) > 0) {
+  if (isset($item['title']) && drupal_strlen($item['title']) > 0) {
     $image['title'] = $item['title'];
     $image['alt'] = $item['title'];
   }
 
-  $output = theme('image', $image);
+  // Use imagecache external if it's available.
+  if(module_exists('imagecache_external'))
+    $output = theme('imagecache_external', $image);
+  else
+    $output = theme('image', $image);
+
+  // Show title as caption below the rendered image.
+  if ($item['show_caption'] && isset($item['title']) && drupal_strlen($item['title']) > 0) {
+    $attributes = array();
+    $attributes['class'] = 'imgcaption';
+
+    $output = $output . '<div ' . drupal_attributes($attributes) . '>' . $item['title'] . '</div>';
+  }
 
   return $output;
 }
-- 
1.8.0.msysgit.0

