| 1 |
<?php
|
| 2 |
// $Id: filefield_image.module,v 1.6 2008/06/29 18:27:53 jpetso Exp $
|
| 3 |
/**
|
| 4 |
* @file
|
| 5 |
* FileField Image: Image support for CCK file fields.
|
| 6 |
*
|
| 7 |
* Copyright 2008 by Jakob Petsovits <jpetso@gmx.at>
|
| 8 |
* Distributed under the GNU General Public Licence version 2 or higher,
|
| 9 |
* as published by the FSF on http://www.gnu.org/copyleft/gpl.html
|
| 10 |
*/
|
| 11 |
|
| 12 |
/**
|
| 13 |
* Implementation of hook_elements().
|
| 14 |
*/
|
| 15 |
function filefield_image_elements() {
|
| 16 |
$elements = array();
|
| 17 |
$elements['filefield_image_edit'] = array(
|
| 18 |
'#input' => TRUE,
|
| 19 |
'#process' => array('filefield_image_edit_process'),
|
| 20 |
);
|
| 21 |
return $elements;
|
| 22 |
}
|
| 23 |
|
| 24 |
/**
|
| 25 |
* Implementation of hook_theme().
|
| 26 |
*/
|
| 27 |
function filefield_image_theme() {
|
| 28 |
return array(
|
| 29 |
'filefield_image_edit' => array(
|
| 30 |
'arguments' => array('element' => NULL),
|
| 31 |
),
|
| 32 |
'filefield_image_file_formatter_default' => array(
|
| 33 |
'arguments' => array(
|
| 34 |
'file' => NULL, 'field' => NULL, 'file_formatter_settings' => NULL,
|
| 35 |
),
|
| 36 |
),
|
| 37 |
'filefield_image' => array(
|
| 38 |
'arguments' => array(
|
| 39 |
'file' => NULL, 'attributes' => array(), 'getsize' => TRUE,
|
| 40 |
),
|
| 41 |
),
|
| 42 |
);
|
| 43 |
}
|
| 44 |
|
| 45 |
/**
|
| 46 |
* Suitability callback the image widget and formatter.
|
| 47 |
* Handles the three web mimetypes: jpeg, png and gif.
|
| 48 |
*/
|
| 49 |
function filefield_image_handles_file($file) {
|
| 50 |
return in_array($file->filemime, array(
|
| 51 |
'image/jpeg', 'image/png', 'image/gif', // proper mimetypes
|
| 52 |
'image/jpg', 'image/pjpeg', // IE nonstandard mimetypes, wtf?
|
| 53 |
));
|
| 54 |
}
|
| 55 |
|
| 56 |
/**
|
| 57 |
* Implementation of filefield's hook_file_widget_info().
|
| 58 |
*/
|
| 59 |
function filefield_image_file_widget_info() {
|
| 60 |
$module_path = drupal_get_path('module', 'filefield_image');
|
| 61 |
|
| 62 |
return array(
|
| 63 |
'default' => array(
|
| 64 |
'form element' => 'filefield_image_edit',
|
| 65 |
'suitability callback' => 'filefield_image_edit_handles_file',
|
| 66 |
'css' => array($module_path .'/filefield_image.css'),
|
| 67 |
'title' => t('Images (png/jpeg/gif)'),
|
| 68 |
'description' => t('An edit widget for image files, including a preview of the image.'),
|
| 69 |
),
|
| 70 |
);
|
| 71 |
}
|
| 72 |
|
| 73 |
function filefield_image_edit_handles_file($file, $field, $field_widget) {
|
| 74 |
return filefield_image_handles_file($file);
|
| 75 |
}
|
| 76 |
|
| 77 |
/**
|
| 78 |
* The 'process' callback for 'filefield_image_edit' form elements.
|
| 79 |
* Called after defining the form and while building it, transforms the
|
| 80 |
* barebone element array into an preview and and a text field for editing
|
| 81 |
* the image properties (.
|
| 82 |
*/
|
| 83 |
function filefield_image_edit_process($element, $edit, &$form_state, $form) {
|
| 84 |
$field = $element['#field'];
|
| 85 |
$delta = $element['#delta'];
|
| 86 |
$file = $element['#file'];
|
| 87 |
$url = file_create_url($file->filepath);
|
| 88 |
|
| 89 |
$prefix = isset($element['#prefix']) ? $element['#prefix'] : '';
|
| 90 |
$suffix = isset($element['#suffix']) ? $element['#suffix'] : '';
|
| 91 |
$element['#prefix'] = $prefix .'<div class="filefield-image-edit">';
|
| 92 |
$element['#suffix'] = '</div>'. $suffix;
|
| 93 |
|
| 94 |
$title = isset($file->title) ? $file->title : '';
|
| 95 |
|
| 96 |
$element['preview'] = array(
|
| 97 |
'#type' => 'markup',
|
| 98 |
'#value' => theme('filefield_image', $file, array('width' => '150'), FALSE),
|
| 99 |
);
|
| 100 |
$element['info'] = array(
|
| 101 |
'#type' => 'item',
|
| 102 |
'#description' => t('Size: !size. Filename: !link', array(
|
| 103 |
'!size' => format_size($file->filesize),
|
| 104 |
'!link' => l($file->filename, $url),
|
| 105 |
)),
|
| 106 |
'#prefix' => '<div class="filefield-image-edit-info filefield-image-edit-control">',
|
| 107 |
'#suffix' => '</div>',
|
| 108 |
);
|
| 109 |
$element['description'] = array(
|
| 110 |
'#type' => 'textfield',
|
| 111 |
'#title' => t('Description'),
|
| 112 |
'#description' => t('Alternate text to be displayed if the image cannot be displayed.'),
|
| 113 |
'#default_value' => empty($file->description)
|
| 114 |
? $file->filename
|
| 115 |
: $file->description,
|
| 116 |
'#maxlength' => 256,
|
| 117 |
'#required' => TRUE,
|
| 118 |
'#prefix' => '<div class="filefield-image-edit-description filefield-image-edit-control">',
|
| 119 |
'#suffix' => '</div>',
|
| 120 |
);
|
| 121 |
return $element;
|
| 122 |
}
|
| 123 |
|
| 124 |
|
| 125 |
/**
|
| 126 |
* Implementation of filefield's hook_file_formatter_info().
|
| 127 |
*/
|
| 128 |
function filefield_image_file_formatter_info() {
|
| 129 |
$module_path = drupal_get_path('module', 'filefield_image');
|
| 130 |
|
| 131 |
return array(
|
| 132 |
'default' => array(
|
| 133 |
'suitability callback' => 'filefield_image_formatter_handles_file',
|
| 134 |
'css' => array($module_path .'/filefield_image.css'),
|
| 135 |
'title' => t('Images (png/jpeg/gif, original size)'),
|
| 136 |
'description' => t('Displays image files in their original size.'),
|
| 137 |
),
|
| 138 |
);
|
| 139 |
}
|
| 140 |
|
| 141 |
function filefield_image_formatter_handles_file($file, $field) {
|
| 142 |
return filefield_image_handles_file($file);
|
| 143 |
}
|
| 144 |
|
| 145 |
/**
|
| 146 |
* Theme function for the 'filefield_image_edit' form element.
|
| 147 |
*/
|
| 148 |
function theme_filefield_image_edit($element) {
|
| 149 |
return theme('form_element', $element, $element['#children']);
|
| 150 |
}
|
| 151 |
|
| 152 |
/**
|
| 153 |
* Theme function for the single image file formatter.
|
| 154 |
*/
|
| 155 |
function theme_filefield_image_file_formatter_default($file, $field, $file_formatter_settings) {
|
| 156 |
return theme('filefield_image', $file);
|
| 157 |
}
|
| 158 |
|
| 159 |
/**
|
| 160 |
* Theme function for displaying an file as \<img\> tag, optionally with
|
| 161 |
* extra attributes for the tag and automatic size recognition.
|
| 162 |
*/
|
| 163 |
function theme_filefield_image($file, $attributes = array(), $getsize = TRUE) {
|
| 164 |
if ($getsize) {
|
| 165 |
list($width, $height, $type, $image_attributes) = @getimagesize($file->filepath);
|
| 166 |
}
|
| 167 |
if (empty($image_attributes)) {
|
| 168 |
if (!is_file($file->filepath)) {
|
| 169 |
return '';
|
| 170 |
}
|
| 171 |
$image_attributes = '';
|
| 172 |
}
|
| 173 |
$description = empty($file->description) ? $file->filename : $file->description;
|
| 174 |
|
| 175 |
$attributes = array_merge($attributes, array(
|
| 176 |
'alt' => $description,
|
| 177 |
'title' => empty($file->title) ? $description : $file->title,
|
| 178 |
));
|
| 179 |
$attributes = $image_attributes .' '. drupal_attributes($attributes);
|
| 180 |
$url = file_create_url($file->filepath);
|
| 181 |
|
| 182 |
return '<div class="filefield-image">'.
|
| 183 |
'<img src="'. check_url($url) .'" '. $attributes .' /></div>';
|
| 184 |
}
|