1 image.field.inc image_field_instance_settings_form($field, $instance)

Implements hook_field_instance_settings_form().

File

core/modules/image/image.field.inc, line 83
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('Images larger than these dimensions (width × height) will be resized to these limits. This will cause the loss of <a href="http://en.wikipedia.org/wiki/Exchangeable_image_file_format">EXIF data</a> in the image. Leave blank for no restriction.'),
  );
  $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' => ' × ',
    '#placeholder' => t('Width'),
  );
  $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'),
    '#placeholder' => t('Height'),
  );

  $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('Images smaller than these dimensions (width × height) will be rejected. Leave blank for no restriction. '),
  );
  $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' => ' × ',
    '#placeholder' => t('Width'),
  );
  $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'),
    '#placeholder' => t('Height'),
  );

  // 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['alt_field_required'] = array(
    '#type' => 'checkbox',
    '#title' => t('<em>Alt</em> field required'),
    '#default_value' => !isset($settings['alt_field_required']) ? 0 : $settings['alt_field_required'],
    '#description' => t('Requiring alternative text is strongly encouraged to improve accessibility.'),
    '#indentation' => 1,
    '#weight' => 11,
    '#states' => array(
      'visible' => array(
        'input[name="instance[settings][alt_field]"]' => array('checked' => TRUE),
      ),
    ),
  );
  $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. Enabling this field is not recommended, as it can cause problems with screen readers.'),
    '#weight' => 12,
  );
  $form['orientate'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable image re-orientation'),
    '#default_value' => $settings['orientate'],
    '#weight' => 13,
    '#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' => 'checkbox',
      '#title' => t('Remove default image'),
    );
  }

  return $form;
}