1 image.field.inc | image_field_widget_process($element, &$form_state, $form) |
An element #process callback for the image_image field type.
Expands the image_image type to include the alt and title fields.
File
- core/
modules/ image/ image.field.inc, line 491 - 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['fid'] = $element['fid']['#value'];
$instance = field_widget_instance($element, $form_state);
$settings = $instance['settings'];
$widget_settings = $instance['widget']['settings'];
$element['#theme'] = 'image_widget';
$element['#attached']['css'][] = backdrop_get_path('module', 'image') . '/css/image.css';
// Add the image preview.
if ($element['#file'] && $widget_settings['preview_image_style']) {
$variables = array(
'style_name' => $widget_settings['preview_image_style'],
'uri' => $element['#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($element['#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'],
);
}
// 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['fid'] && $settings['alt_field'],
);
$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['fid'] && $settings['title_field'],
);
if ($settings['alt_field_required']) {
// Adding #required directly to the element would trigger unnecessary
// validation errors when uploading the image before submitting the form.
// Rather than adding manual markup to the #title to mimic the required
// asterisk indication, we are adding an element #pre_render attribute,
// and then adding the #required property to the function that it calls.
// That way, it is rendered, but it doesn't actually trigger any validation.
$element['alt']['#pre_render'] = array('_image_field_widget_alt_pre_render');
$element['alt']['#element_validate'] = array('_image_field_widget_alt_validate');
// Pass the alt required status as a data-* attribute on the form element,
// in case other modules need access to it.
$element['alt']['#attributes'] = array('data-alt-required' => 'true');
}
return $element;
}