function ImageWidget::process
Same name in other branches
- 9 core/modules/image/src/Plugin/Field/FieldWidget/ImageWidget.php \Drupal\image\Plugin\Field\FieldWidget\ImageWidget::process()
- 8.9.x core/modules/image/src/Plugin/Field/FieldWidget/ImageWidget.php \Drupal\image\Plugin\Field\FieldWidget\ImageWidget::process()
- 10 core/modules/image/src/Plugin/Field/FieldWidget/ImageWidget.php \Drupal\image\Plugin\Field\FieldWidget\ImageWidget::process()
Form API callback: Processes an image_image field element.
Expands the image_image type to include the alt and title fields.
This method is assigned as a #process callback in formElement() method.
Overrides FileWidget::process
File
-
core/
modules/ image/ src/ Plugin/ Field/ FieldWidget/ ImageWidget.php, line 197
Class
- ImageWidget
- Plugin implementation of the 'image_image' widget.
Namespace
Drupal\image\Plugin\Field\FieldWidgetCode
public static function process($element, FormStateInterface $form_state, $form) {
$item = $element['#value'];
$item['fids'] = $element['fids']['#value'];
$element['#theme'] = 'image_widget';
// Add the image preview.
if (!empty($element['#files']) && $element['#preview_image_style']) {
$file = reset($element['#files']);
$variables = [
'style_name' => $element['#preview_image_style'],
'uri' => $file->getFileUri(),
];
$dimension_key = $variables['uri'] . '.image_preview_dimensions';
// Determine image dimensions.
if (isset($element['#value']['width']) && isset($element['#value']['height'])) {
$variables['width'] = $element['#value']['width'];
$variables['height'] = $element['#value']['height'];
}
elseif ($form_state->has($dimension_key)) {
$variables += $form_state->get($dimension_key);
}
else {
$image = \Drupal::service('image.factory')->get($file->getFileUri());
if ($image->isValid()) {
$variables['width'] = $image->getWidth();
$variables['height'] = $image->getHeight();
}
else {
$variables['width'] = $variables['height'] = NULL;
}
}
$element['preview'] = [
'#weight' => -10,
'#theme' => 'image_style',
'#width' => $variables['width'],
'#height' => $variables['height'],
'#style_name' => $variables['style_name'],
'#uri' => $variables['uri'],
];
// Store the dimensions in the form so the file doesn't have to be
// accessed again. This is important for remote files.
$form_state->set($dimension_key, [
'width' => $variables['width'],
'height' => $variables['height'],
]);
}
elseif (!empty($element['#default_image'])) {
$default_image = $element['#default_image'];
$file = File::load($default_image['fid']);
if (!empty($file)) {
$element['preview'] = [
'#weight' => -10,
'#theme' => 'image_style',
'#width' => $default_image['width'],
'#height' => $default_image['height'],
'#style_name' => $element['#preview_image_style'],
'#uri' => $file->getFileUri(),
];
}
}
// Add the additional alt and title fields.
$element['alt'] = [
'#title' => new TranslatableMarkup('Alternative text'),
'#type' => 'textfield',
'#default_value' => $item['alt'] ?? '',
'#description' => new TranslatableMarkup('Short description of the image used by screen readers and displayed when the image is not loaded. This is important for accessibility.'),
// @see https://www.drupal.org/node/465106#alt-text
'#maxlength' => 512,
'#weight' => -12,
'#access' => (bool) $item['fids'] && $element['#alt_field'],
'#required' => $element['#alt_field_required'],
'#element_validate' => $element['#alt_field_required'] == 1 ? [
[
static::class,
'validateRequiredFields',
],
] : [],
];
$element['title'] = [
'#type' => 'textfield',
'#title' => new TranslatableMarkup('Title'),
'#default_value' => $item['title'] ?? '',
'#description' => new TranslatableMarkup('The title is used as a tool tip when the user hovers the mouse over the image.'),
'#maxlength' => 1024,
'#weight' => -11,
'#access' => (bool) $item['fids'] && $element['#title_field'],
'#required' => $element['#title_field_required'],
'#element_validate' => $element['#title_field_required'] == 1 ? [
[
static::class,
'validateRequiredFields',
],
] : [],
];
return parent::process($element, $form_state, $form);
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.