public static function SvgImageFieldWidget::process in SVG Image Field 8
Same name and namespace in other branches
- 2.1.x src/Plugin/Field/FieldWidget/SvgImageFieldWidget.php \Drupal\svg_image_field\Plugin\Field\FieldWidget\SvgImageFieldWidget::process()
- 2.0.x src/Plugin/Field/FieldWidget/SvgImageFieldWidget.php \Drupal\svg_image_field\Plugin\Field\FieldWidget\SvgImageFieldWidget::process()
Form API callback: Processes a 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
- src/
Plugin/ Field/ FieldWidget/ SvgImageFieldWidget.php, line 180
Class
- SvgImageFieldWidget
- Plugin implementation of the 'image_image' widget.
Namespace
Drupal\svg_image_field\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_max_width'] || $element['#preview_image_max_height'])) {
$file = reset($element['#files']);
$attributes = [
'style' => '',
];
if (!empty($element['#preview_image_max_width'])) {
$attributes['style'] = "max-width: {$element['#preview_image_max_width']}px;";
}
if (!empty($element['#preview_image_max_height'])) {
$attributes['style'] .= "max-height: {$element['#preview_image_max_height']}px;";
}
if (!empty($item['alt'])) {
$attributes['alt'] = $item['alt'];
$attributes['title'] = $attributes['alt'];
}
$element['preview'] = [
'#theme' => 'svg_image_field_formatter',
'#inline' => FALSE,
'#attributes' => $attributes,
'#uri' => $file
->getFileUri(),
'#svg_data' => NULL,
];
}
elseif (!empty($element['#default_image'])) {
$default_image = $element['#default_image'];
$file = File::load($default_image['fid']);
if (!empty($file)) {
$attributes = [
'style' => '',
];
if (!empty($element['#preview_image_max_width'])) {
$attributes['style'] = "max-width={$element['#preview_image_max_width']}px;";
}
if (!empty($element['#preview_image_max_height'])) {
$attributes['style'] .= "max-height={$element['#preview_image_max_height']}px;";
}
$element['preview'] = [
'#weight' => -10,
'#theme' => 'svg_image_field_formatter',
'#inline' => FALSE,
'#attributes' => $attributes,
'#uri' => $file
->getFileUri(),
'#svg_data' => NULL,
];
}
}
// 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, $form_state, $form);
}