View source
<?php
function auto_image_style_field_formatter_info() {
return array(
'auto_image_style_default' => array(
'label' => t('Image auto orientation'),
'field types' => array(
'image',
),
'settings' => array(
'image_style_portrait' => 'thumbnail',
'image_style_portrait_class' => 'portrait',
'image_style_landscape' => 'thumbnail',
'image_style_landscape_class' => 'landscape',
'image_style_configure_css' => FALSE,
'image_link' => '',
),
'description' => t('Display image fields as portrait or landscape style'),
),
);
}
function auto_image_style_field_formatter_settings_form($field, $instance, $view_mode) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
$form = array();
$options = image_style_options(FALSE, PASS_THROUGH);
if (module_exists('picture')) {
$options = array_merge($options, auto_image_style_picture_mapping_names());
$form['picture_mapping_link'] = array(
'#title' => t('Picture mapping'),
'#type' => 'item',
'#markup' => l(t('Create a picture mapping'), 'admin/config/media/picture/add'),
'#description' => t('You can add new the Picture mapping and use it as image portrait or landscape style.'),
);
}
else {
$form['picture_mapping_link'] = array(
'#type' => 'item',
'#markup' => l(t('You also can use the Picture module for output responsive images'), 'https://www.drupal.org/project/picture', array(
'absolute' => TRUE,
'attributes' => array(
'title' => 'yadda, ydadda,yadda',
),
)),
);
}
$form['image_style_portrait'] = array(
'#type' => 'select',
'#title' => t('Select the portrait style'),
'#options' => $options,
'#empty_option' => t('None (original image)'),
'#default_value' => $settings['image_style_portrait'],
'#description' => t('Select the imagecache style for portrait images'),
);
$form['image_style_landscape'] = array(
'#type' => 'select',
'#title' => t('Select the landscape style'),
'#options' => $options,
'#empty_option' => t('None (original image)'),
'#default_value' => $settings['image_style_landscape'],
'#description' => t('Select the imagecache style for landscape images'),
);
$form['image_style_configure_css'] = array(
'#type' => 'checkbox',
'#title' => t('Configure CSS class'),
'#default_value' => $settings['image_style_configure_css'],
);
$form['image_style_portrait_class'] = array(
'#type' => 'textfield',
'#title' => t('CSS Class for portrait'),
'#default_value' => $settings['image_style_portrait_class'],
'#size' => 60,
'#maxlength' => 128,
'#required' => TRUE,
'#process' => array(
'_auto_image_style_formatter_settings_form_process_add_state',
),
);
$form['image_style_landscape_class'] = array(
'#type' => 'textfield',
'#title' => t('CSS Class for landscape'),
'#default_value' => $settings['image_style_landscape_class'],
'#size' => 60,
'#maxlength' => 128,
'#required' => TRUE,
'#process' => array(
'_auto_image_style_formatter_settings_form_process_add_state',
),
);
$link_types = array(
'content' => t('Content'),
'file' => t('File'),
);
if (module_exists('link')) {
$link_types = array_merge($link_types, auto_image_style_get_link_fields($instance));
}
$form['image_link'] = array(
'#title' => t('Link image to'),
'#type' => 'select',
'#default_value' => $settings['image_link'],
'#empty_option' => t('Nothing'),
'#options' => $link_types,
);
return $form;
}
function _auto_image_style_formatter_settings_form_process_add_state($element, $form_state) {
$target_parents = array_slice($element['#parents'], 0, -1);
$target_parents[] = 'image_style_configure_css';
$target_parents = array_shift($target_parents) . ($target_parents ? '[' . implode('][', $target_parents) . ']' : '');
$element['#states']['visible'] = array(
':input[name="' . $target_parents . '"]' => array(
'checked' => TRUE,
),
);
return $element;
}
function auto_image_style_picture_mapping_names() {
$mapping_names = array();
foreach (picture_mapping_load_all() as $mapping_name => $value) {
$mapping_names['picture_mapping_' . $mapping_name] = t('Automatic picture(@mapping_name)', array(
'@mapping_name' => $mapping_name,
));
}
return $mapping_names;
}
function auto_image_style_get_link_fields($instance) {
$link_types = array();
foreach (field_info_instances($instance['entity_type'], $instance['bundle']) as $name => $instance) {
$info = field_info_field($name);
if ($info['type'] == 'link_field') {
$link_types[$name] = "{$instance['label']} ({$instance['field_name']})";
}
}
return $link_types;
}
function auto_image_style_field_formatter_settings_summary($field, $instance, $view_mode) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
$summary = array();
$summary[] = t('Image style for portrait: @value', array(
'@value' => $settings['image_style_portrait'],
));
$summary[] = t('Image style for landscape: @value', array(
'@value' => $settings['image_style_landscape'],
));
$link_types = array(
'content' => t('content'),
'file' => t('file'),
);
if (module_exists('link')) {
$link_types = array_merge($link_types, auto_image_style_get_link_fields($instance));
}
if ($settings['image_style_configure_css']) {
$summary[] = t('CSS class for portrait: @value', array(
'@value' => $settings['image_style_portrait_class'],
));
$summary[] = t('CSS class for landscape: @value', array(
'@value' => $settings['image_style_landscape_class'],
));
}
if (isset($link_types[$settings['image_link']])) {
$summary[] = t('Linked to @type', array(
'@type' => $link_types[$settings['image_link']],
));
}
return implode('<br />', $summary);
}
function auto_image_style_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
if (empty($items)) {
return array();
}
$element = array();
$settings = $display['settings'];
$link_to_file = FALSE;
$link_to_link = FALSE;
if ($display['settings']['image_link'] == 'content') {
$uri = entity_uri($entity_type, $entity);
}
elseif ($display['settings']['image_link'] == 'file') {
$link_to_file = TRUE;
}
elseif (isset($entity->{$display['settings']['image_link']})) {
$link_to_link = TRUE;
$link_to_link_values = field_get_items($entity_type, $entity, $display['settings']['image_link'], $langcode);
}
$image_styles = image_styles();
foreach ($items as $delta => $item) {
if ($link_to_file) {
$uri = array(
'path' => file_create_url($item['uri']),
'options' => array(),
);
}
if ($link_to_link && isset($link_to_link_values[$delta])) {
$uri = array(
'path' => $link_to_link_values[$delta]['url'],
'options' => array(),
);
}
if ($item['height'] < $item['width']) {
$image_style = $settings['image_style_landscape'];
$item['attributes']['class'] = array(
'auto_image_style',
$settings['image_style_landscape_class'],
);
}
else {
$image_style = $settings['image_style_portrait'];
$item['attributes']['class'] = array(
'auto_image_style',
$settings['image_style_portrait_class'],
);
}
$element[$delta] = array(
'#theme' => 'image_formatter',
'#item' => $item,
'#image_style' => $image_style,
'#path' => isset($uri) ? $uri : '',
);
if (strpos($image_style, 'picture_mapping_') !== false) {
$mapping_name = substr($image_style, 16);
$fallback_image_style = '';
$picture_mapping = picture_mapping_load($mapping_name);
$breakpoint_styles = picture_get_mapping_breakpoints($picture_mapping, $fallback_image_style);
$formatter = 'picture_formatter';
$colorbox_breakpoints = array();
$colorbox_fallback_image_style = '';
if (module_exists('picture')) {
$element[$delta] = array(
'#theme' => 'picture_formatter',
'#attached' => array(
'library' => array(
array(
'picture',
'picturefill_head',
),
array(
'picture',
'picturefill',
),
array(
'picture',
'picture.ajax',
),
),
),
'#item' => $item,
'#image_style' => $fallback_image_style,
'#breakpoints' => $breakpoint_styles,
'#path' => isset($uri) ? $uri : '',
'#colorbox_group' => $colorbox_breakpoints,
'#colorbox_image_style' => $colorbox_fallback_image_style,
);
}
}
}
return $element;
}