public static function SvgImageWidget::process in Svg Image 8
Same name and namespace in other branches
- 2.x src/Plugin/Field/FieldWidget/SvgImageWidget.php \Drupal\svg_image\Plugin\Field\FieldWidget\SvgImageWidget::process()
- 1.x src/Plugin/Field/FieldWidget/SvgImageWidget.php \Drupal\svg_image\Plugin\Field\FieldWidget\SvgImageWidget::process()
Form API callback: Processes a file_generic field element.
Expands the file_generic type to include the description and display fields.
This method is assigned as a #process callback in formElement() method.
Overrides FileWidget::process
File
- src/
Plugin/ Field/ FieldWidget/ SvgImageWidget.php, line 226
Class
- SvgImageWidget
- Override plugin of the 'image_image' widget.
Namespace
Drupal\svg_image\Plugin\Field\FieldWidgetCode
public static function process($element, FormStateInterface $formState, $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 = svg_image_get_image_file_dimensions($file);
$variables['style_name'] = $element['#preview_image_style'];
$variables['uri'] = $file
->getFileUri();
// Add a custom preview for SVG file.
if (svg_image_is_file_svg($file)) {
$element['preview'] = [
'#weight' => -10,
'#theme' => 'image',
'#width' => $variables['width'],
'#height' => $variables['height'],
'#uri' => $variables['uri'],
];
}
else {
$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.
$element['width'] = [
'#type' => 'hidden',
'#value' => $variables['width'],
];
$element['height'] = [
'#type' => 'hidden',
'#value' => $variables['height'],
];
}
elseif (!empty($element['#default_image'])) {
$defaultImage = $element['#default_image'];
$file = File::load($defaultImage['fid']);
if (!empty($file)) {
$element['preview'] = [
'#weight' => -10,
'#theme' => 'image_style',
'#width' => $defaultImage['width'],
'#height' => $defaultImage['height'],
'#style_name' => $element['#preview_image_style'],
'#uri' => $file
->getFileUri(),
];
}
}
// Add the additional alt and title fields.
$element['alt'] = [
'#title' => t('Alternative 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 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 ? [
[
get_called_class(),
'validateRequiredFields',
],
] : [],
];
$element['title'] = [
'#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' => -11,
'#access' => (bool) $item['fids'] && $element['#title_field'],
'#required' => $element['#title_field_required'],
'#element_validate' => $element['#title_field_required'] == 1 ? [
[
get_called_class(),
'validateRequiredFields',
],
] : [],
];
return parent::process($element, $formState, $form);
}