1.20.x image.field.inc image_field_instance_settings_form($field, $instance)

Implements hook_field_instance_settings_form().

File

modules/image/image.field.inc, line 82
Implement an image field, based on the file module's file field.

Code

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);

  $form['#element_validate'] = isset($form['#element_validate']) ? $form['#element_validate'] : array();
  $form['#element_validate'][] = 'image_field_instance_settings_form_validate';

  // Add maximum and minimum resolution settings.
  $max_dimensions = explode('x', $settings['max_dimensions']) + array('', '');
  $max_dimensions[0] = empty($max_dimensions[0]) ? NULL : $max_dimensions[0];
  $max_dimensions[1] = empty($max_dimensions[1]) ? NULL : $max_dimensions[1];
  $form['max_dimensions'] = array(
    '#type' => 'item',
    '#title' => t('Maximum image dimensions'),
    '#element_validate' => array('_image_field_resolution_validate'),
    '#weight' => 4.1,
    '#field_prefix' => '<div class="container-inline">',
    '#field_suffix' => '</div>',
    '#description' => t('The maximum allowed image dimensions expressed as <code>WIDTH</code> x <code>HEIGHT</code>. 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_dimensions']['x'] = array(
    '#type' => 'number',
    '#title' => t('Maximum width'),
    '#title_display' => 'invisible',
    '#default_value' => $max_dimensions[0],
    '#min' => 0,
    '#max' => 100000,
    '#field_suffix' => ' x ',
  );
  $form['max_dimensions']['y'] = array(
    '#type' => 'number',
    '#title' => t('Maximum height'),
    '#title_display' => 'invisible',
    '#default_value' => $max_dimensions[1],
    '#min' => 0,
    '#max' => 100000,
    '#field_suffix' => ' ' . t('pixels'),
  );

  $min_dimensions = explode('x', $settings['min_dimensions']) + array('', '');
  $min_dimensions[0] = empty($min_dimensions[0]) ? NULL : $min_dimensions[0];
  $min_dimensions[1] = empty($min_dimensions[1]) ? NULL : $min_dimensions[1];
  $form['min_dimensions'] = array(
    '#type' => 'item',
    '#title' => t('Minimum image dimensions'),
    '#element_validate' => array('_image_field_resolution_validate'),
    '#weight' => 4.2,
    '#field_prefix' => '<div class="container-inline">',
    '#field_suffix' => '</div>',
    '#description' => t('The minimum allowed image dimensions expressed as <code>WIDTH</code> x <code>HEIGHT</code>. Leave blank for no restriction. If a smaller image is uploaded, it will be rejected.'),
  );
  $form['min_dimensions']['x'] = array(
    '#type' => 'number',
    '#title' => t('Minimum width'),
    '#title_display' => 'invisible',
    '#default_value' => $min_dimensions[0],
    '#min' => 0,
    '#max' => 100000,
    '#field_suffix' => ' x ',
  );
  $form['min_dimensions']['y'] = array(
    '#type' => 'number',
    '#title' => t('Minimum height'),
    '#title_display' => 'invisible',
    '#default_value' => $min_dimensions[1],
    '#min' => 0,
    '#max' => 100000,
    '#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,
  );
  $form['orientate'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable image re-orientation'),
    '#default_value' => $settings['orientate'],
    '#weight' => 12,
    '#description' => t('If EXIF data indicates the need, the image will be rotated appropriately before being saved.'),
  );
  // Add a second line of description.
  $form['orientate']['#description'] .= '<br />' . t('Note that as with resizing, rotating images on upload will cause the loss of EXIF data in the image.');

  // Add the default image to the instance.
  $form['default_image'] = array(
    '#element_validate' => array('_image_field_default_image_validate'),
    '#default_scheme' => $field['settings']['uri_scheme'],
    '#target' => array('instance', 'settings', 'default_image'),
  );
  $form['default_image']['file'] = array(
    '#title' => t('Default image'),
    '#type' => 'file',
    '#description' => t("If no image is uploaded, this image will be shown on display and will override the field's default image."),
  );
  if ($settings['default_image']) {
    $link = l(basename($settings['default_image']), file_create_url($settings['default_image']), array('attributes' => array('target' => '_blank')));
    $form['default_image']['file']['#description'] .= '<br />' . t('Current image: !link', array('!link' => $link));

    $form['default_image']['remove'] = array(
      '#type' => t('checkbox'),
      '#title' => t('Remove default image'),
    );
  }

  return $form;
}