You are here

auto_image_style.module in Auto image style 7

File

auto_image_style.module
View source
<?php

/**
 * Implements hook_field_formatter_info().
 */
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'),
    ),
  );
}

/**
 * Implements hook_field_formatter_settings_form().
 */
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;
}

/**
 * Helper function: set the proper #states to the use widget handlers.
 */
function _auto_image_style_formatter_settings_form_process_add_state($element, $form_state) {

  // Build a #parents based on the current checkbox.
  $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;
}

/**
 * Helper function return array of picture mappings
 */
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;
}

/**
 * Helper function return array of link fields
 */
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;
}

/**
 * Implements hook_field_formatter_settings_summary().
 */
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'],
    ));
  }

  // Display this setting only if image is linked.
  if (isset($link_types[$settings['image_link']])) {
    $summary[] = t('Linked to @type', array(
      '@type' => $link_types[$settings['image_link']],
    ));
  }
  return implode('<br />', $summary);
}

/**
 * Implements hook_field_formatter_view().
 */
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;

  // Check if the formatter involves a link.
  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);
  }

  //all image styles
  $image_styles = image_styles();
  foreach ($items as $delta => $item) {

    // link to image file
    if ($link_to_file) {
      $uri = array(
        'path' => file_create_url($item['uri']),
        'options' => array(),
      );
    }

    // link module integration
    if ($link_to_link && isset($link_to_link_values[$delta])) {
      $uri = array(
        'path' => $link_to_link_values[$delta]['url'],
        'options' => array(),
      );
    }

    // landscape image
    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);

      // Assume regular display.
      $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;
}