| 1 |
<?php
|
| 2 |
// $Id: linkimagefield.module,v 1.6 2009/07/25 00:26:19 johnfyoung Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Defines a link image field type.
|
| 7 |
*
|
| 8 |
* 6.x Port - Mainly wraps the ImageField CCK Type, adding a URL field
|
| 9 |
* The URL is stored (like the image alt and title tags) in the filefield's data element
|
| 10 |
*
|
| 11 |
* TODO:
|
| 12 |
* - for some reason the display formatter is not being set as the default
|
| 13 |
* -
|
| 14 |
*/
|
| 15 |
|
| 16 |
/**
|
| 17 |
* Implementation of hook_init().
|
| 18 |
*
|
| 19 |
* Load required includes.
|
| 20 |
*/
|
| 21 |
function linkimagefield_init() {
|
| 22 |
// If Content, FileField, or ImageField is not available, immediately disable Link Image Field.
|
| 23 |
$disable = FALSE;
|
| 24 |
$message = '';
|
| 25 |
|
| 26 |
if (!module_exists('content')) {
|
| 27 |
$disable = TRUE;
|
| 28 |
$message = t('The <a href="http://drupal.org/project/cck">Content Creation Kit</a> needs to be installed for it to work properly.');
|
| 29 |
}
|
| 30 |
|
| 31 |
if (!module_exists('filefield')) {
|
| 32 |
$disable = TRUE;
|
| 33 |
$message = (!empty($message) ? '<br />' : '') . t('The <a href="http://drupal.org/project/filefield">FileField module</a> needs to be installed for it to work properly.');
|
| 34 |
}
|
| 35 |
|
| 36 |
if (!module_exists('imagefield')) {
|
| 37 |
$disable = TRUE;
|
| 38 |
$message = (!empty($message) ? '<br />' : '') . t('The <a href="http://drupal.org/project/imagefield">ImageField module</a> needs to be installed for it to work properly.');
|
| 39 |
}
|
| 40 |
|
| 41 |
if ($disable) {
|
| 42 |
module_disable(array('linkimagefield'));
|
| 43 |
drupal_set_message(t('The Link Image Field module has been disabled.<br />') . $message);
|
| 44 |
return;
|
| 45 |
}
|
| 46 |
|
| 47 |
// Load include code
|
| 48 |
module_load_include('inc', 'linkimagefield', 'linkimagefield_widget');
|
| 49 |
}
|
| 50 |
|
| 51 |
|
| 52 |
/**
|
| 53 |
* Implementation of hook_elements().
|
| 54 |
*/
|
| 55 |
function linkimagefield_elements() {
|
| 56 |
$elements = array();
|
| 57 |
|
| 58 |
// An LinkImageField is really just a ImageField with a URL.
|
| 59 |
$imagefield_elements = imagefield_elements();
|
| 60 |
$elements['linkimagefield_widget'] = $imagefield_elements['imagefield_widget'];
|
| 61 |
$elements['linkimagefield_widget']['#process'][] = 'linkimagefield_widget_process';
|
| 62 |
$elements['linkimagefield_widget']['#element_validate'][] = 'linkimagefield_widget_validate';
|
| 63 |
|
| 64 |
// ImageField needs a separate value callback to save its alt, title and URL texts.
|
| 65 |
$elements['linkimagefield_widget']['#value_callback'] = 'linkimagefield_widget_value';
|
| 66 |
|
| 67 |
return $elements;
|
| 68 |
}
|
| 69 |
|
| 70 |
function linkimagefield_theme() {
|
| 71 |
$theme = array(
|
| 72 |
'linkimagefield_image' => array(
|
| 73 |
'arguments' => array('file' => NULL, 'alt' => '', 'title' => '', 'attributes' => NULL, 'getsize' => TRUE, 'url' => '', 'target' => '_self'),
|
| 74 |
),
|
| 75 |
'linkimagefield_justlink' => array(
|
| 76 |
'arguments' => array('url' => '', 'target' => '_self', 'title' => '', 'class' => ''),
|
| 77 |
),
|
| 78 |
|
| 79 |
// Theme a Link Image Field field item. It calls linkimagefied_image with the proper
|
| 80 |
// item properties as arguments.
|
| 81 |
'linkimagefield_item' => array(
|
| 82 |
'arguments' => array('item' => NULL),
|
| 83 |
),
|
| 84 |
// linkimagefield_widget form element type theme function.
|
| 85 |
'linkimagefield_widget' => array(
|
| 86 |
'arguments' => array('element' => NULL),
|
| 87 |
'file' => 'linkimagefield_widget.inc',
|
| 88 |
),
|
| 89 |
// Use to generate a preview (admin view) of an linkimagefield item for use in
|
| 90 |
// field item forms and filefield widgets. Invoked by filefield_widget_process.
|
| 91 |
'linkimagefield_widget_preview' => array(
|
| 92 |
'arguments' => array('item' => NULL),
|
| 93 |
),
|
| 94 |
// Theme function for the field item elements. allows you to place children
|
| 95 |
// within the context of the parent.
|
| 96 |
'linkimagefield_widget_item' => array(
|
| 97 |
'arguments' => array('element' => NULL),
|
| 98 |
),
|
| 99 |
// Generates and img tag to the admin thumbnail of an LinkImageField upload.
|
| 100 |
'linkimagefield_admin_thumbnail' => array(
|
| 101 |
'arguments' => array('item' => NULL),
|
| 102 |
),
|
| 103 |
'linkimagefield_formatter_linkimage' => array(
|
| 104 |
'arguments' => array('element' => NULL),
|
| 105 |
'file' => 'linkimagefield_formatter.inc',
|
| 106 |
),
|
| 107 |
'linkimagefield_formatter_linkimage_justlink' => array(
|
| 108 |
'arguments' => array('element' => NULL),
|
| 109 |
'file' => 'linkimagefield_formatter.inc',
|
| 110 |
),
|
| 111 |
);
|
| 112 |
|
| 113 |
/**
|
| 114 |
* to override these themes the function call would look like:
|
| 115 |
*
|
| 116 |
* mytheme_linkimagefield_formatter_imagecachepresetname_linkimage
|
| 117 |
*
|
| 118 |
* where the imagecachepresetname is the name of the desired imagecache preset
|
| 119 |
*/
|
| 120 |
if(module_exists('imagecache')) {
|
| 121 |
$imagecache_presets = module_invoke('imagecache', 'presets');
|
| 122 |
foreach ($imagecache_presets as $preset) {
|
| 123 |
$theme['linkimagefield_formatter_'. $preset['presetname'] .'_linkimage'] = array(
|
| 124 |
'arguments' => array('element' => NULL),
|
| 125 |
'function' => 'theme_linkimagefield_imagecache',
|
| 126 |
);
|
| 127 |
}
|
| 128 |
}
|
| 129 |
|
| 130 |
return $theme;
|
| 131 |
}
|
| 132 |
|
| 133 |
/**
|
| 134 |
* Implementation of hook_views_api().
|
| 135 |
*/
|
| 136 |
// TODO: get views data to work
|
| 137 |
/*
|
| 138 |
function linkimagefield_views_api() {
|
| 139 |
return array(
|
| 140 |
'api' => 2.0,
|
| 141 |
'path' => drupal_get_path('module', 'linkimagefield') . '/views',
|
| 142 |
);
|
| 143 |
}*/
|
| 144 |
|
| 145 |
/**
|
| 146 |
* Implementation of hook_file_download.
|
| 147 |
*
|
| 148 |
* @param string $filepath
|
| 149 |
*/
|
| 150 |
function linkimagefield_file_download($filepath) {
|
| 151 |
// Delegate to ImageField
|
| 152 |
imagefield_file_download($filepath);
|
| 153 |
}
|
| 154 |
|
| 155 |
/**
|
| 156 |
* Implementation of CCK's hook_widget_info().
|
| 157 |
*
|
| 158 |
* @return array
|
| 159 |
*/
|
| 160 |
function linkimagefield_widget_info() {
|
| 161 |
$module_path = drupal_get_path('module', 'linkimagefield');
|
| 162 |
return array(
|
| 163 |
'linkimagefield_widget' => array(
|
| 164 |
'label' => t('Link Image'),
|
| 165 |
'field types' => array('filefield'),
|
| 166 |
'multiple values' => CONTENT_HANDLE_CORE,
|
| 167 |
'callbacks' => array('default value' => CONTENT_CALLBACK_CUSTOM),
|
| 168 |
'description' => t('An edit widget for image files that display as a link, including a preview of the image.'),
|
| 169 |
),
|
| 170 |
);
|
| 171 |
}
|
| 172 |
|
| 173 |
/**
|
| 174 |
* Implementation of CCK's hook_widget_settings().
|
| 175 |
*
|
| 176 |
* @param string $op
|
| 177 |
* @param array $widget
|
| 178 |
* @return array
|
| 179 |
*/
|
| 180 |
function linkimagefield_widget_settings($op, $widget) {
|
| 181 |
switch ($op) {
|
| 182 |
case 'form':
|
| 183 |
return linkimagefield_widget_settings_form($widget);
|
| 184 |
case 'validate':
|
| 185 |
return linkimagefield_widget_settings_validate($widget);
|
| 186 |
case 'save':
|
| 187 |
return linkimagefield_widget_settings_save($widget);
|
| 188 |
}
|
| 189 |
}
|
| 190 |
|
| 191 |
/**
|
| 192 |
* Implementation of CCK's hook_widget
|
| 193 |
*
|
| 194 |
* @param array $form
|
| 195 |
* @param array $form_state
|
| 196 |
* @param array $field
|
| 197 |
* @param array $items
|
| 198 |
* @param integer $delta
|
| 199 |
* @return array
|
| 200 |
*/
|
| 201 |
function linkimagefield_widget(&$form, &$form_state, $field, $items, $delta = 0) {
|
| 202 |
if (empty($items[$delta])) {
|
| 203 |
$items[$delta] = array('url' => '');
|
| 204 |
}
|
| 205 |
|
| 206 |
// Delegate to ImageField
|
| 207 |
$element = imagefield_widget($form, $form_state, $field, $items, $delta);
|
| 208 |
|
| 209 |
return $element;
|
| 210 |
}
|
| 211 |
|
| 212 |
/**
|
| 213 |
* Implementation of hook_field_formatter_info
|
| 214 |
*
|
| 215 |
* @return array
|
| 216 |
*/
|
| 217 |
function linkimagefield_field_formatter_info() {
|
| 218 |
$formatters['linkimage'] = array(
|
| 219 |
'label' => t('Link Image'),
|
| 220 |
'field types' => array('filefield'),
|
| 221 |
'description' => t('Displays image files as link to provided URL.'),
|
| 222 |
);
|
| 223 |
|
| 224 |
$formatters['linkimage_justlink'] = array(
|
| 225 |
'label' => t('Link Image - Just Link'),
|
| 226 |
'field types' => array('filefield'),
|
| 227 |
'description' => t('Displays just the link supplied for a Link Image Field.'),
|
| 228 |
);
|
| 229 |
|
| 230 |
if(module_exists('imagecache')) {
|
| 231 |
$imagecache_presets = module_invoke('imagecache', 'presets');
|
| 232 |
|
| 233 |
foreach ($imagecache_presets as $preset) {
|
| 234 |
$formatters[$preset['presetname'] .'_linkimage'] = array(
|
| 235 |
'label' => t('@preset Link Image', array('@preset' => $preset['presetname'])),
|
| 236 |
'field types' => array('image', 'filefield'),
|
| 237 |
);
|
| 238 |
}
|
| 239 |
}
|
| 240 |
|
| 241 |
return $formatters;
|
| 242 |
}
|
| 243 |
|
| 244 |
/**
|
| 245 |
* Implementation of hook_form_[form_id]_alter().
|
| 246 |
*
|
| 247 |
* Modify the add new field form to make "LinkImage" the default formatter.
|
| 248 |
*/
|
| 249 |
function linkimagefield_form_content_field_overview_form_alter(&$form, &$form_state) {
|
| 250 |
$form['#submit'][] = 'linkimagefield_form_content_field_overview_submit';
|
| 251 |
}
|
| 252 |
|
| 253 |
/**
|
| 254 |
* Submit handler to set a new field's formatter to "linkimage_urllink".
|
| 255 |
*/
|
| 256 |
function linkimagefield_form_content_field_overview_submit(&$form, &$form_state) {
|
| 257 |
if (isset($form_state['fields_added']['_add_new_field']) && isset($form['#type_name'])) {
|
| 258 |
$new_field = $form_state['fields_added']['_add_new_field'];
|
| 259 |
$node_type = $form['#type_name'];
|
| 260 |
$field = content_fields($new_field, $node_type);
|
| 261 |
if ($field['widget']['module'] == 'linkimagefield') {
|
| 262 |
foreach ($field['display_settings'] as $display_type => $display_settings) {
|
| 263 |
if ($field['display_settings'][$display_type]['format'] == 'default') {
|
| 264 |
$field['display_settings'][$display_type]['format'] = 'linkimage_urllink';
|
| 265 |
}
|
| 266 |
}
|
| 267 |
content_field_instance_update($field);
|
| 268 |
}
|
| 269 |
}
|
| 270 |
}
|
| 271 |
|
| 272 |
/**
|
| 273 |
* Implementation of CCK's hook_default_value().
|
| 274 |
*/
|
| 275 |
function linkimagefield_default_value(&$form, &$form_state, $field, $delta) {
|
| 276 |
return imagefield_default_value($form, $form_state, $field, $delta);
|
| 277 |
}
|
| 278 |
|
| 279 |
/**
|
| 280 |
* determine whether an attribute for the link should be the default or the custom
|
| 281 |
*
|
| 282 |
* works for a url, alt or title attribute
|
| 283 |
*
|
| 284 |
* @param string $attr
|
| 285 |
* @param array $item
|
| 286 |
* @param array $field
|
| 287 |
* @return string
|
| 288 |
*/
|
| 289 |
function _linkimagefield_format_set_attribute($attr, $item, $field) {
|
| 290 |
$attr_value = '';
|
| 291 |
|
| 292 |
// if custom values are allowed, use the user supplied value or default if one's not supplied
|
| 293 |
// else since no custom values are allowed, just use the default if supplied
|
| 294 |
// (URL is a special case - doesn't have a check for use custom value)
|
| 295 |
if(($field['widget']['custom_'.$attr]) || $attr == 'url' || $attr == 'target') {
|
| 296 |
if($attr == 'target' && $item['data'][$attr] == 'default') {
|
| 297 |
$item['data'][$attr] = $field['widget'][$attr];
|
| 298 |
}
|
| 299 |
$attr_value = !empty($item['data'][$attr]) ? $item['data'][$attr] : (!empty($field['widget'][$attr]) ? $field['widget'][$attr] : '');
|
| 300 |
} else {
|
| 301 |
$attr_value = !empty($field['widget'][$attr]) ? $field['widget'][$attr] : ($attr == 'target' ? '_self' : '');
|
| 302 |
}
|
| 303 |
|
| 304 |
return $attr_value;
|
| 305 |
}
|
| 306 |
|
| 307 |
function theme_linkimagefield_formatter($element) {
|
| 308 |
if (isset($element['#item']['nid']) && $node = node_load($element['#item']['nid'])) {
|
| 309 |
return linkimagefield_field_formatter($element['#field_name'], $element['#item'], $element['#formatter'], $node);
|
| 310 |
}
|
| 311 |
}
|
| 312 |
|
| 313 |
function theme_linkimagefield_image($file, $alt = '', $title = '', $attributes = NULL, $getsize = TRUE, $url = '', $target = '_self', $nofollow = FALSE) {
|
| 314 |
$url_attributes = array('title' => $title);
|
| 315 |
if($target != '_self') {
|
| 316 |
$url_attributes['target'] = $target;
|
| 317 |
}
|
| 318 |
if($nofollow) {
|
| 319 |
$url_attributes['rel'] = 'nofollow';
|
| 320 |
}
|
| 321 |
$imagetag = theme('imagefield_image', $file, $alt, $title, $attributes, $getsize);
|
| 322 |
if(empty($url)) {
|
| 323 |
return $imagetag;
|
| 324 |
}
|
| 325 |
return l($imagetag, $url, array('attributes' => $url_attributes, 'html' => TRUE));
|
| 326 |
}
|
| 327 |
|
| 328 |
function theme_linkimagefield_justlink($url = '', $target = '_self', $title = '', $class = '', $nofollow = FALSE) {
|
| 329 |
$url_attributes = array('class' => $class, 'title' => $title);
|
| 330 |
if($target != '_self') {
|
| 331 |
$url_attributes['target'] = $target;
|
| 332 |
}
|
| 333 |
if($nofollow) {
|
| 334 |
$url_attributes['rel'] = 'nofollow';
|
| 335 |
}
|
| 336 |
return l($url, $url, array('attributes' => $url_attributes, 'html' => TRUE));
|
| 337 |
}
|
| 338 |
|
| 339 |
function theme_linkimagefield_imagecache($element) {
|
| 340 |
$field = content_fields($element['#field_name']);
|
| 341 |
$item = $element['#item'];
|
| 342 |
$formatter = $element['#formatter'];
|
| 343 |
|
| 344 |
if (empty($item['fid']) && $field['use_default_image']) {
|
| 345 |
$item = $field['default_image'];
|
| 346 |
} else if (empty($item['fid'])) { // mod by eric
|
| 347 |
return '';
|
| 348 |
}
|
| 349 |
// Views does not load the file for us, while CCK display fields does.
|
| 350 |
if (empty($item['filepath'])) {
|
| 351 |
$item = array_merge($item, field_file_load($item['fid']));
|
| 352 |
}
|
| 353 |
if (is_string($item['data'])) {
|
| 354 |
$item['data'] = unserialize($item['data']);
|
| 355 |
}
|
| 356 |
|
| 357 |
$alt = _linkimagefield_format_set_attribute('alt', $item, $field);
|
| 358 |
$title = _linkimagefield_format_set_attribute('title', $item, $field);
|
| 359 |
$url = _linkimagefield_format_set_attribute('url', $item, $field);
|
| 360 |
$target = _linkimagefield_format_set_attribute('target', $item, $field);
|
| 361 |
$nofollow = $item['data']['nofollow'];
|
| 362 |
$parts = explode('_', $formatter);
|
| 363 |
$style = array_pop($parts);
|
| 364 |
$presetname = implode('_', $parts);
|
| 365 |
|
| 366 |
$class = "linkimagefield imagecache imagecache-$presetname imagecache-$style imagecache-$formatter";
|
| 367 |
if ($preset = imagecache_preset_by_name($presetname)) {
|
| 368 |
$item['filepath'] = $item['fid'] == 'upload' ? $item['preview'] : $item['filepath'];
|
| 369 |
$imagetag = theme('imagecache', $presetname, $item['filepath'], $alt, $title);
|
| 370 |
$attributes = array('class' => $class, 'title' => $title);
|
| 371 |
if($target != '_self') {
|
| 372 |
$attributes['target'] = $target;
|
| 373 |
}
|
| 374 |
if($nofollow) {
|
| 375 |
$attributes['rel'] = 'nofollow';
|
| 376 |
}
|
| 377 |
if(empty($url)) {
|
| 378 |
return $imagetag;
|
| 379 |
}
|
| 380 |
return l($imagetag, $url, array('attributes' => $attributes, 'html' => TRUE));
|
| 381 |
}
|
| 382 |
return '<!-- linkimagefield formatter imagecache preset('. $presetname .') not found! -->';
|
| 383 |
}
|
| 384 |
|
| 385 |
function theme_linkimagefield_item($item) {
|
| 386 |
return theme('linkimagefield_image', $item, $item['alt'], $item['title'], TRUE, $item[$url], $item[$target]);
|
| 387 |
}
|
| 388 |
|
| 389 |
function theme_linkimagefield_widget_item($element) {
|
| 390 |
return theme('imagefield_widget_item', $element);
|
| 391 |
}
|
| 392 |
|
| 393 |
function theme_linkimagefield_widget_preview($item = NULL) {
|
| 394 |
return '<div class="imagefield-preview">'. theme('imagefield_admin_thumbnail', $item) .'</div>';
|
| 395 |
}
|
| 396 |
|
| 397 |
function theme_linkimagefield_admin_thumbnail($item = NULL) {
|
| 398 |
return theme_imagefield_admin_thumbnail($item);
|
| 399 |
}
|