function image_field_widget_process

An element #process callback for the image_image field type.

Expands the image_image type to include the alt and title fields.

1 string reference to 'image_field_widget_process'
ImageWidget::formElement in drupal/core/modules/image/lib/Drupal/image/Plugin/field/widget/ImageWidget.php
Overrides \Drupal\file\Plugin\field\widget\FileWidget::formElement().

File

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

Code

function image_field_widget_process($element, &$form_state, $form) {
  $item = $element['#value'];
  $item['fids'] = $element['fids']['#value'];
  $element['#theme'] = 'image_widget';
  $element['#attached']['css'][] = drupal_get_path('module', 'image') . '/css/image.theme.css';

  // Add the image preview.
  if (!empty($element['#files']) && $element['#preview_image_style']) {
    $file = reset($element['#files']);
    $variables = array(
      'style_name' => $element['#preview_image_style'],
      'uri' => $file->uri,
    );

    // 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($file->uri);
      if (is_array($info)) {
        $variables['width'] = $info['width'];
        $variables['height'] = $info['height'];
      }
      else {
        $variables['width'] = $variables['height'] = NULL;
      }
    }
    $element['preview'] = array(
      '#type' => 'markup',
      '#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'],
    );
  }

  // Get field settings.
  $instance = field_widget_instance($element, $form_state);
  $settings = $instance['settings'];

  // 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.'),
    // @see http://www.gawds.org/show.php?contentid=28
    '#maxlength' => 512,
    '#weight' => -2,
    '#access' => (bool) $item['fids'] && $element['#alt_field'],
    '#element_validate' => $settings['alt_field_required'] == 1 ? array(
      '_image_field_required_fields_validate',
    ) : array(),
  );
  $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.'),
    '#maxlength' => 1024,
    '#weight' => -1,
    '#access' => (bool) $item['fids'] && $element['#title_field'],
    '#element_validate' => $settings['alt_field_required'] == 1 ? array(
      '_image_field_required_fields_validate',
    ) : array(),
  );
  return $element;
}