2009-10-12 05:22:57 +00:00
< ? php
/**
* @ file
* Implement an image field , based on the file module ' s file field .
*/
/**
2009-12-04 16:49:48 +00:00
* Implements hook_field_info () .
2009-10-12 05:22:57 +00:00
*/
function image_field_info () {
return array (
'image' => array (
'label' => t ( 'Image' ),
'description' => t ( 'This field stores the ID of an image file as an integer value.' ),
'settings' => array (
2010-09-09 23:28:16 +00:00
'uri_scheme' => variable_get ( 'file_default_scheme' , 'public' ),
2009-10-12 05:22:57 +00:00
'default_image' => 0 ,
),
'instance_settings' => array (
'file_extensions' => 'png gif jpg jpeg' ,
'file_directory' => '' ,
'max_filesize' => '' ,
'alt_field' => 0 ,
'title_field' => 0 ,
'max_resolution' => '' ,
'min_resolution' => '' ,
2012-04-24 02:03:13 +00:00
'default_image' => 0 ,
2009-10-12 05:22:57 +00:00
),
'default_widget' => 'image_image' ,
'default_formatter' => 'image' ,
),
);
}
/**
2009-12-04 16:49:48 +00:00
* Implements hook_field_settings_form () .
2009-10-12 05:22:57 +00:00
*/
function image_field_settings_form ( $field , $instance ) {
$defaults = field_info_field_settings ( $field [ 'type' ]);
$settings = array_merge ( $defaults , $field [ 'settings' ]);
$scheme_options = array ();
2010-01-26 08:29:25 +00:00
foreach ( file_get_stream_wrappers ( STREAM_WRAPPERS_WRITE_VISIBLE ) as $scheme => $stream_wrapper ) {
$scheme_options [ $scheme ] = $stream_wrapper [ 'name' ];
2009-10-12 05:22:57 +00:00
}
$form [ 'uri_scheme' ] = array (
'#type' => 'radios' ,
'#title' => t ( 'Upload destination' ),
'#options' => $scheme_options ,
'#default_value' => $settings [ 'uri_scheme' ],
'#description' => t ( 'Select where the final files should be stored. Private file storage has significantly more overhead than public files, but allows restricted access to files within this field.' ),
);
2011-06-15 04:13:14 +00:00
// When the user sets the scheme on the UI, even for the first time, it's
// updating a field because fields are created on the "Manage fields"
// page. So image_field_update_field() can handle this change.
2009-10-12 05:22:57 +00:00
$form [ 'default_image' ] = array (
'#title' => t ( 'Default image' ),
'#type' => 'managed_file' ,
'#description' => t ( 'If no image is uploaded, this image will be shown on display.' ),
'#default_value' => $field [ 'settings' ][ 'default_image' ],
2011-06-15 04:13:14 +00:00
'#upload_location' => $settings [ 'uri_scheme' ] . '://default_images/' ,
2009-10-12 05:22:57 +00:00
);
return $form ;
}
/**
2009-12-04 16:49:48 +00:00
* Implements hook_field_instance_settings_form () .
2009-10-12 05:22:57 +00:00
*/
function image_field_instance_settings_form ( $field , $instance ) {
$settings = $instance [ 'settings' ];
// Use the file field instance settings form as a basis.
$form = file_field_instance_settings_form ( $field , $instance );
// Add maximum and minimum resolution settings.
$max_resolution = explode ( 'x' , $settings [ 'max_resolution' ]) + array ( '' , '' );
$form [ 'max_resolution' ] = array (
2010-08-30 06:19:42 +00:00
'#type' => 'item' ,
2009-10-12 05:22:57 +00:00
'#title' => t ( 'Maximum image resolution' ),
'#element_validate' => array ( '_image_field_resolution_validate' ),
'#weight' => 4.1 ,
2010-08-30 06:19:42 +00:00
'#field_prefix' => '<div class="container-inline">' ,
'#field_suffix' => '</div>' ,
2009-10-12 05:22:57 +00:00
'#description' => t ( 'The maximum allowed image size expressed as WIDTHxHEIGHT (e.g. 640x480). Leave blank for no restriction. If a larger image is uploaded, it will be resized to reflect the given width and height. Resizing images on upload will cause the loss of <a href="http://en.wikipedia.org/wiki/Exchangeable_image_file_format">EXIF data</a> in the image.' ),
);
$form [ 'max_resolution' ][ 'x' ] = array (
2012-04-19 11:31:47 +00:00
'#type' => 'number' ,
2010-10-20 01:31:07 +00:00
'#title' => t ( 'Maximum width' ),
'#title_display' => 'invisible' ,
2009-10-12 05:22:57 +00:00
'#default_value' => $max_resolution [ 0 ],
2012-04-19 11:31:47 +00:00
'#min' => 1 ,
2009-10-12 05:22:57 +00:00
'#field_suffix' => ' x ' ,
);
$form [ 'max_resolution' ][ 'y' ] = array (
2012-04-19 11:31:47 +00:00
'#type' => 'number' ,
2010-10-20 01:31:07 +00:00
'#title' => t ( 'Maximum height' ),
'#title_display' => 'invisible' ,
2009-10-12 05:22:57 +00:00
'#default_value' => $max_resolution [ 1 ],
2012-04-19 11:31:47 +00:00
'#min' => 1 ,
2009-10-12 05:22:57 +00:00
'#field_suffix' => ' ' . t ( 'pixels' ),
);
$min_resolution = explode ( 'x' , $settings [ 'min_resolution' ]) + array ( '' , '' );
$form [ 'min_resolution' ] = array (
2010-08-30 06:19:42 +00:00
'#type' => 'item' ,
2009-10-12 05:22:57 +00:00
'#title' => t ( 'Minimum image resolution' ),
'#element_validate' => array ( '_image_field_resolution_validate' ),
'#weight' => 4.2 ,
2010-08-30 06:19:42 +00:00
'#field_prefix' => '<div class="container-inline">' ,
'#field_suffix' => '</div>' ,
2009-10-12 05:22:57 +00:00
'#description' => t ( 'The minimum allowed image size expressed as WIDTHxHEIGHT (e.g. 640x480). Leave blank for no restriction. If a smaller image is uploaded, it will be rejected.' ),
);
$form [ 'min_resolution' ][ 'x' ] = array (
2012-04-19 11:31:47 +00:00
'#type' => 'number' ,
2010-10-20 01:31:07 +00:00
'#title' => t ( 'Minimum width' ),
'#title_display' => 'invisible' ,
2009-10-12 05:22:57 +00:00
'#default_value' => $min_resolution [ 0 ],
2012-04-19 11:31:47 +00:00
'#min' => 1 ,
2009-10-12 05:22:57 +00:00
'#field_suffix' => ' x ' ,
);
$form [ 'min_resolution' ][ 'y' ] = array (
2012-04-19 11:31:47 +00:00
'#type' => 'number' ,
2010-10-20 01:31:07 +00:00
'#title' => t ( 'Minimum height' ),
'#title_display' => 'invisible' ,
2009-10-12 05:22:57 +00:00
'#default_value' => $min_resolution [ 1 ],
2012-04-19 11:31:47 +00:00
'#min' => 1 ,
2009-10-12 05:22:57 +00:00
'#field_suffix' => ' ' . t ( 'pixels' ),
);
// Remove the description option.
unset ( $form [ 'description_field' ]);
// Add title and alt configuration options.
$form [ 'alt_field' ] = array (
'#type' => 'checkbox' ,
'#title' => t ( 'Enable <em>Alt</em> field' ),
'#default_value' => $settings [ 'alt_field' ],
'#description' => t ( 'The alt attribute may be used by search engines, screen readers, and when the image cannot be loaded.' ),
'#weight' => 10 ,
);
$form [ 'title_field' ] = array (
'#type' => 'checkbox' ,
'#title' => t ( 'Enable <em>Title</em> field' ),
'#default_value' => $settings [ 'title_field' ],
'#description' => t ( 'The title attribute is used as a tooltip when the mouse hovers over the image.' ),
'#weight' => 11 ,
);
2012-04-24 02:03:13 +00:00
// Add the default image to the instance.
$form [ 'default_image' ] = array (
'#title' => t ( 'Default image' ),
'#type' => 'managed_file' ,
'#description' => t ( " If no image is uploaded, this image will be shown on display and will override the field's default image. " ),
'#default_value' => $settings [ 'default_image' ],
'#upload_location' => $field [ 'settings' ][ 'uri_scheme' ] . '://default_images/' ,
);
2009-10-12 05:22:57 +00:00
return $form ;
}
/**
* Element validate function for resolution fields .
*/
function _image_field_resolution_validate ( $element , & $form_state ) {
if ( ! empty ( $element [ 'x' ][ '#value' ]) || ! empty ( $element [ 'y' ][ '#value' ])) {
foreach ( array ( 'x' , 'y' ) as $dimension ) {
2012-04-19 11:31:47 +00:00
if ( ! $element [ $dimension ][ '#value' ]) {
2009-10-12 05:22:57 +00:00
form_error ( $element [ $dimension ], t ( 'Both a height and width value must be specified in the !name field.' , array ( '!name' => $element [ '#title' ])));
return ;
}
}
2012-04-19 11:31:47 +00:00
form_set_value ( $element , $element [ 'x' ][ '#value' ] . 'x' . $element [ 'y' ][ '#value' ], $form_state );
2009-10-12 05:22:57 +00:00
}
else {
form_set_value ( $element , '' , $form_state );
}
}
/**
2009-12-04 16:49:48 +00:00
* Implements hook_field_load () .
2009-10-12 05:22:57 +00:00
*/
2010-02-11 17:44:47 +00:00
function image_field_load ( $entity_type , $entities , $field , $instances , $langcode , & $items , $age ) {
file_field_load ( $entity_type , $entities , $field , $instances , $langcode , $items , $age );
2009-10-12 05:22:57 +00:00
}
/**
2009-12-13 12:42:28 +00:00
* Implements hook_field_prepare_view () .
2009-10-12 05:22:57 +00:00
*/
2010-02-11 17:44:47 +00:00
function image_field_prepare_view ( $entity_type , $entities , $field , $instances , $langcode , & $items ) {
2009-10-12 05:22:57 +00:00
// If there are no files specified at all, use the default.
2010-02-11 17:44:47 +00:00
foreach ( $entities as $id => $entity ) {
2012-04-24 02:03:13 +00:00
if ( empty ( $items [ $id ])) {
$fid = 0 ;
// Use the default for the instance if one is available.
2012-05-03 17:37:15 +00:00
if ( ! empty ( $instances [ $id ][ 'settings' ][ 'default_image' ])) {
2012-04-24 02:03:13 +00:00
$fid = $instances [ $id ][ 'settings' ][ 'default_image' ];
}
// Otherwise, use the default for the field.
2012-05-03 17:37:15 +00:00
elseif ( ! empty ( $field [ 'settings' ][ 'default_image' ])) {
2012-04-24 02:03:13 +00:00
$fid = $field [ 'settings' ][ 'default_image' ];
}
// Add the default image if one is found.
if ( $fid && ( $file = file_load ( $fid ))) {
2009-12-13 12:42:28 +00:00
$items [ $id ][ 0 ] = ( array ) $file + array (
'is_default' => TRUE ,
'alt' => '' ,
'title' => '' ,
);
}
2009-10-12 05:22:57 +00:00
}
}
}
2010-02-17 05:39:51 +00:00
/**
* Implements hook_field_presave () .
*/
2010-03-27 05:52:50 +00:00
function image_field_presave ( $entity_type , $entity , $field , $instance , $langcode , & $items ) {
2011-10-17 16:34:18 +00:00
// Determine the dimensions if necessary.
foreach ( $items as & $item ) {
if ( ! isset ( $item [ 'width' ]) || ! isset ( $item [ 'height' ])) {
$info = image_get_info ( file_load ( $item [ 'fid' ]) -> uri );
if ( is_array ( $info )) {
$item [ 'width' ] = $info [ 'width' ];
$item [ 'height' ] = $info [ 'height' ];
}
}
}
2010-02-17 05:39:51 +00:00
}
2010-08-22 13:52:59 +00:00
/**
* Implements hook_field_insert () .
*/
function image_field_insert ( $entity_type , $entity , $field , $instance , $langcode , & $items ) {
file_field_insert ( $entity_type , $entity , $field , $instance , $langcode , $items );
}
2009-10-12 05:22:57 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_field_update () .
2009-10-12 05:22:57 +00:00
*/
2010-02-11 17:44:47 +00:00
function image_field_update ( $entity_type , $entity , $field , $instance , $langcode , & $items ) {
file_field_update ( $entity_type , $entity , $field , $instance , $langcode , $items );
2009-10-12 05:22:57 +00:00
}
/**
2009-12-04 16:49:48 +00:00
* Implements hook_field_delete () .
2009-10-12 05:22:57 +00:00
*/
2010-02-11 17:44:47 +00:00
function image_field_delete ( $entity_type , $entity , $field , $instance , $langcode , & $items ) {
file_field_delete ( $entity_type , $entity , $field , $instance , $langcode , $items );
2009-10-12 05:22:57 +00:00
}
/**
2009-12-04 16:49:48 +00:00
* Implements hook_field_delete_revision () .
2009-10-12 05:22:57 +00:00
*/
2010-02-11 17:44:47 +00:00
function image_field_delete_revision ( $entity_type , $entity , $field , $instance , $langcode , & $items ) {
file_field_delete_revision ( $entity_type , $entity , $field , $instance , $langcode , $items );
2009-10-12 05:22:57 +00:00
}
/**
2009-12-04 16:49:48 +00:00
* Implements hook_field_is_empty () .
2009-10-12 05:22:57 +00:00
*/
function image_field_is_empty ( $item , $field ) {
return file_field_is_empty ( $item , $field );
}
/**
2009-12-04 16:49:48 +00:00
* Implements hook_field_widget_info () .
2009-10-12 05:22:57 +00:00
*/
function image_field_widget_info () {
return array (
'image_image' => array (
'label' => t ( 'Image' ),
'field types' => array ( 'image' ),
'settings' => array (
'progress_indicator' => 'throbber' ,
'preview_image_style' => 'thumbnail' ,
),
'behaviors' => array (
'multiple values' => FIELD_BEHAVIOR_CUSTOM ,
'default value' => FIELD_BEHAVIOR_NONE ,
),
),
);
}
/**
2009-12-04 16:49:48 +00:00
* Implements hook_field_widget_settings_form () .
2009-10-12 05:22:57 +00:00
*/
function image_field_widget_settings_form ( $field , $instance ) {
$widget = $instance [ 'widget' ];
$settings = $widget [ 'settings' ];
// Use the file widget settings form.
$form = file_field_widget_settings_form ( $field , $instance );
$form [ 'preview_image_style' ] = array (
'#title' => t ( 'Preview image style' ),
'#type' => 'select' ,
2010-09-24 21:36:22 +00:00
'#options' => image_style_options ( FALSE ),
2010-10-20 16:19:24 +00:00
'#empty_option' => '<' . t ( 'no preview' ) . '>' ,
2009-10-12 05:22:57 +00:00
'#default_value' => $settings [ 'preview_image_style' ],
'#description' => t ( 'The preview image will be shown while editing the content.' ),
'#weight' => 15 ,
);
return $form ;
}
/**
2009-12-21 13:47:32 +00:00
* Implements hook_field_widget_form () .
2009-10-12 05:22:57 +00:00
*/
2009-12-21 13:47:32 +00:00
function image_field_widget_form ( & $form , & $form_state , $field , $instance , $langcode , $items , $delta , $element ) {
2010-02-23 09:52:56 +00:00
// Add display_field setting to field because file_field_widget_form() assumes it is set.
$field [ 'settings' ][ 'display_field' ] = 0 ;
2009-12-21 13:47:32 +00:00
$elements = file_field_widget_form ( $form , $form_state , $field , $instance , $langcode , $items , $delta , $element );
2009-10-12 05:22:57 +00:00
$settings = $instance [ 'settings' ];
foreach ( element_children ( $elements ) as $delta ) {
// Add upload resolution validation.
if ( $settings [ 'max_resolution' ] || $settings [ 'min_resolution' ]) {
$elements [ $delta ][ '#upload_validators' ][ 'file_validate_image_resolution' ] = array ( $settings [ 'max_resolution' ], $settings [ 'min_resolution' ]);
}
// If not using custom extension validation, ensure this is an image.
$supported_extensions = array ( 'png' , 'gif' , 'jpg' , 'jpeg' );
$extensions = isset ( $elements [ $delta ][ '#upload_validators' ][ 'file_validate_extensions' ][ 0 ]) ? $elements [ $delta ][ '#upload_validators' ][ 'file_validate_extensions' ][ 0 ] : implode ( ' ' , $supported_extensions );
$extensions = array_intersect ( explode ( ' ' , $extensions ), $supported_extensions );
$elements [ $delta ][ '#upload_validators' ][ 'file_validate_extensions' ][ 0 ] = implode ( ' ' , $extensions );
// Add all extra functionality provided by the image widget.
$elements [ $delta ][ '#process' ][] = 'image_field_widget_process' ;
}
if ( $field [ 'cardinality' ] == 1 ) {
// If there's only one field, return it as delta 0.
if ( empty ( $elements [ 0 ][ '#default_value' ][ 'fid' ])) {
$elements [ 0 ][ '#description' ] = theme ( 'file_upload_help' , array ( 'description' => $instance [ 'description' ], 'upload_validators' => $elements [ 0 ][ '#upload_validators' ]));
}
}
else {
$elements [ '#file_upload_description' ] = theme ( 'file_upload_help' , array ( 'upload_validators' => $elements [ 0 ][ '#upload_validators' ]));
}
return $elements ;
}
/**
* An element #process callback for the image_image field type.
*
* Expands the image_image type to include the alt and title fields .
*/
function image_field_widget_process ( $element , & $form_state , $form ) {
$item = $element [ '#value' ];
$item [ 'fid' ] = $element [ 'fid' ][ '#value' ];
2010-10-31 12:12:00 +00:00
$instance = field_widget_instance ( $element , $form_state );
2010-02-11 15:42:14 +00:00
2009-10-12 05:22:57 +00:00
$settings = $instance [ 'settings' ];
$widget_settings = $instance [ 'widget' ][ 'settings' ];
$element [ '#theme' ] = 'image_widget' ;
$element [ '#attached' ][ 'css' ][] = drupal_get_path ( 'module' , 'image' ) . '/image.css' ;
// Add the image preview.
if ( $element [ '#file' ] && $widget_settings [ 'preview_image_style' ]) {
2011-10-17 16:34:18 +00:00
$variables = array (
'style_name' => $widget_settings [ 'preview_image_style' ],
2011-11-25 03:09:40 +00:00
'uri' => $element [ '#file' ] -> uri ,
2011-10-17 16:34:18 +00:00
);
// Determine image dimensions.
if ( isset ( $element [ '#value' ][ 'width' ]) && isset ( $element [ '#value' ][ 'height' ])) {
$variables [ 'width' ] = $element [ '#value' ][ 'width' ];
$variables [ 'height' ] = $element [ '#value' ][ 'height' ];
}
else {
$info = image_get_info ( $element [ '#file' ] -> uri );
if ( is_array ( $info )) {
$variables [ 'width' ] = $info [ 'width' ];
$variables [ 'height' ] = $info [ 'height' ];
}
else {
$variables [ 'width' ] = $variables [ 'height' ] = NULL ;
}
}
2009-10-12 05:22:57 +00:00
$element [ 'preview' ] = array (
'#type' => 'markup' ,
2011-10-17 16:34:18 +00:00
'#markup' => theme ( 'image_style' , $variables ),
);
// Store the dimensions in the form so the file doesn't have to be accessed
// again. This is important for remote files.
$element [ 'width' ] = array (
'#type' => 'hidden' ,
'#value' => $variables [ 'width' ],
);
$element [ 'height' ] = array (
'#type' => 'hidden' ,
'#value' => $variables [ 'height' ],
2009-10-12 05:22:57 +00:00
);
}
// Add the additional alt and title fields.
$element [ 'alt' ] = array (
'#title' => t ( 'Alternate text' ),
'#type' => 'textfield' ,
'#default_value' => isset ( $item [ 'alt' ]) ? $item [ 'alt' ] : '' ,
'#description' => t ( 'This text will be used by screen readers, search engines, or when the image cannot be loaded.' ),
2011-12-13 03:21:08 +00:00
// @see http://www.gawds.org/show.php?contentid=28
2011-12-23 03:16:56 +00:00
'#maxlength' => 512 ,
2009-10-12 05:22:57 +00:00
'#weight' => - 2 ,
'#access' => ( bool ) $item [ 'fid' ] && $settings [ 'alt_field' ],
);
$element [ 'title' ] = array (
'#type' => 'textfield' ,
'#title' => t ( 'Title' ),
'#default_value' => isset ( $item [ 'title' ]) ? $item [ 'title' ] : '' ,
'#description' => t ( 'The title is used as a tool tip when the user hovers the mouse over the image.' ),
2011-12-23 03:16:56 +00:00
'#maxlength' => 1024 ,
2009-10-12 05:22:57 +00:00
'#weight' => - 1 ,
'#access' => ( bool ) $item [ 'fid' ] && $settings [ 'title_field' ],
);
return $element ;
}
/**
2010-04-13 15:23:03 +00:00
* Returns HTML for an image field widget .
*
* @ param $variables
* An associative array containing :
* - element : A render element representing the image field widget .
*
* @ ingroup themeable
2009-10-12 05:22:57 +00:00
*/
function theme_image_widget ( $variables ) {
$element = $variables [ 'element' ];
$output = '' ;
$output .= '<div class="image-widget form-managed-file clearfix">' ;
if ( isset ( $element [ 'preview' ])) {
$output .= '<div class="image-preview">' ;
$output .= drupal_render ( $element [ 'preview' ]);
$output .= '</div>' ;
}
$output .= '<div class="image-widget-data">' ;
if ( $element [ 'fid' ][ '#value' ] != 0 ) {
$element [ 'filename' ][ '#markup' ] .= ' <span class="file-size">(' . format_size ( $element [ '#file' ] -> filesize ) . ')</span> ' ;
}
$output .= drupal_render_children ( $element );
$output .= '</div>' ;
$output .= '</div>' ;
return $output ;
}
/**
2009-12-04 16:49:48 +00:00
* Implements hook_field_formatter_info () .
2009-10-12 05:22:57 +00:00
*/
function image_field_formatter_info () {
$formatters = array (
'image' => array (
'label' => t ( 'Image' ),
'field types' => array ( 'image' ),
2010-08-09 19:55:57 +00:00
'settings' => array ( 'image_style' => '' , 'image_link' => '' ),
2009-10-12 05:22:57 +00:00
),
);
2010-08-09 19:55:57 +00:00
return $formatters ;
}
/**
* Implements hook_field_formatter_settings_form () .
*/
function image_field_formatter_settings_form ( $field , $instance , $view_mode , $form , & $form_state ) {
$display = $instance [ 'display' ][ $view_mode ];
$settings = $display [ 'settings' ];
2010-09-24 21:36:22 +00:00
$image_styles = image_style_options ( FALSE );
2010-10-22 00:42:42 +00:00
$element [ 'image_style' ] = array (
2010-08-09 19:55:57 +00:00
'#title' => t ( 'Image style' ),
'#type' => 'select' ,
'#default_value' => $settings [ 'image_style' ],
2010-09-24 21:36:22 +00:00
'#empty_option' => t ( 'None (original image)' ),
2010-08-09 19:55:57 +00:00
'#options' => $image_styles ,
);
$link_types = array (
'content' => t ( 'Content' ),
'file' => t ( 'File' ),
);
2010-10-22 00:42:42 +00:00
$element [ 'image_link' ] = array (
2010-08-09 19:55:57 +00:00
'#title' => t ( 'Link image to' ),
'#type' => 'select' ,
'#default_value' => $settings [ 'image_link' ],
2010-10-20 16:19:24 +00:00
'#empty_option' => t ( 'Nothing' ),
2010-08-09 19:55:57 +00:00
'#options' => $link_types ,
);
2010-10-22 00:42:42 +00:00
return $element ;
2010-08-09 19:55:57 +00:00
}
/**
* Implements hook_field_formatter_settings_summary () .
*/
function image_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' );
2009-10-12 05:22:57 +00:00
}
2010-08-09 19:55:57 +00:00
$link_types = array (
'content' => t ( 'Linked to content' ),
'file' => t ( 'Linked to file' ),
);
// Display this setting only if image is linked.
if ( isset ( $link_types [ $settings [ 'image_link' ]])) {
$summary [] = $link_types [ $settings [ 'image_link' ]];
}
return implode ( '<br />' , $summary );
2009-10-12 05:22:57 +00:00
}
/**
2009-12-21 13:47:32 +00:00
* Implements hook_field_formatter_view () .
2009-10-12 05:22:57 +00:00
*/
2010-02-11 17:44:47 +00:00
function image_field_formatter_view ( $entity_type , $entity , $field , $instance , $langcode , $items , $display ) {
2009-12-12 20:16:03 +00:00
$element = array ();
2009-10-12 05:22:57 +00:00
2009-12-12 20:16:03 +00:00
// Check if the formatter involves a link.
2010-08-09 19:55:57 +00:00
if ( $display [ 'settings' ][ 'image_link' ] == 'content' ) {
2010-02-11 17:44:47 +00:00
$uri = entity_uri ( $entity_type , $entity );
2009-12-11 16:49:40 +00:00
}
2010-08-09 19:55:57 +00:00
elseif ( $display [ 'settings' ][ 'image_link' ] == 'file' ) {
2009-12-12 20:16:03 +00:00
$link_file = TRUE ;
2009-10-12 05:22:57 +00:00
}
2009-12-12 20:16:03 +00:00
foreach ( $items as $delta => $item ) {
if ( isset ( $link_file )) {
2010-02-11 15:52:13 +00:00
$uri = array (
'path' => file_create_url ( $item [ 'uri' ]),
'options' => array (),
);
2009-12-12 20:16:03 +00:00
}
$element [ $delta ] = array (
'#theme' => 'image_formatter' ,
'#item' => $item ,
2010-08-09 19:55:57 +00:00
'#image_style' => $display [ 'settings' ][ 'image_style' ],
2010-02-11 15:52:13 +00:00
'#path' => isset ( $uri ) ? $uri : '' ,
2009-12-12 20:16:03 +00:00
);
}
2010-02-11 15:52:13 +00:00
return $element ;
2009-10-12 05:22:57 +00:00
}
/**
2010-04-13 15:23:03 +00:00
* Returns HTML for an image field formatter .
*
* @ param $variables
* An associative array containing :
* - item : An array of image data .
* - image_style : An optional image style .
2011-11-25 03:09:40 +00:00
* - path : An optional array containing the link 'path' and link 'options' .
2010-04-13 15:23:03 +00:00
*
* @ ingroup themeable
2009-10-12 05:22:57 +00:00
*/
2009-12-11 16:49:40 +00:00
function theme_image_formatter ( $variables ) {
$item = $variables [ 'item' ];
2011-10-17 16:34:18 +00:00
2010-04-30 12:53:47 +00:00
// Do not output an empty 'title' attribute.
2011-11-25 03:09:40 +00:00
if ( drupal_strlen ( $item [ 'title' ]) == 0 ) {
unset ( $item [ 'title' ]);
2010-04-30 12:53:47 +00:00
}
2009-12-11 16:49:40 +00:00
if ( $variables [ 'image_style' ]) {
2011-11-25 03:09:40 +00:00
$item [ 'style_name' ] = $variables [ 'image_style' ];
$output = theme ( 'image_style' , $item );
2009-12-11 16:49:40 +00:00
}
else {
2011-11-25 03:09:40 +00:00
$output = theme ( 'image' , $item );
2009-12-11 16:49:40 +00:00
}
2011-08-27 08:20:56 +00:00
if ( ! empty ( $variables [ 'path' ][ 'path' ])) {
2010-02-11 15:52:13 +00:00
$path = $variables [ 'path' ][ 'path' ];
$options = $variables [ 'path' ][ 'options' ];
// When displaying an image inside a link, the html option must be TRUE.
$options [ 'html' ] = TRUE ;
$output = l ( $output , $path , $options );
2009-12-11 16:49:40 +00:00
}
return $output ;
2010-01-30 07:59:26 +00:00
}