function ImageItem::fieldSettingsForm
Same name in other branches
- 9 core/modules/image/src/Plugin/Field/FieldType/ImageItem.php \Drupal\image\Plugin\Field\FieldType\ImageItem::fieldSettingsForm()
- 8.9.x core/modules/image/src/Plugin/Field/FieldType/ImageItem.php \Drupal\image\Plugin\Field\FieldType\ImageItem::fieldSettingsForm()
- 11.x core/modules/image/src/Plugin/Field/FieldType/ImageItem.php \Drupal\image\Plugin\Field\FieldType\ImageItem::fieldSettingsForm()
Overrides FileItem::fieldSettingsForm
File
-
core/
modules/ image/ src/ Plugin/ Field/ FieldType/ ImageItem.php, line 213
Class
- ImageItem
- Plugin implementation of the 'image' field type.
Namespace
Drupal\image\Plugin\Field\FieldTypeCode
public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
// Get base form from FileItem.
$element = parent::fieldSettingsForm($form, $form_state);
$settings = $this->getSettings();
// Add maximum and minimum dimensions settings.
$max_resolution = explode('x', $settings['max_resolution']) + [
'',
'',
];
$element['max_resolution'] = [
'#type' => 'item',
'#title' => $this->t('Maximum image dimensions'),
'#element_validate' => [
[
static::class,
'validateResolution',
],
],
'#weight' => 4.1,
'#description' => $this->t('The maximum allowed image size expressed as WIDTH×HEIGHT (e.g. 640×480). Leave blank for no restriction. If a larger image is uploaded, it will be resized to reflect the given width and height. Resizing images on upload will cause the loss of <a href="http://wikipedia.org/wiki/Exchangeable_image_file_format">EXIF data</a> in the image.'),
];
$element['max_resolution']['x'] = [
'#type' => 'number',
'#title' => $this->t('Maximum width'),
'#title_display' => 'invisible',
'#default_value' => $max_resolution[0],
'#min' => 1,
'#field_suffix' => ' × ',
'#prefix' => '<div class="form--inline clearfix">',
];
$element['max_resolution']['y'] = [
'#type' => 'number',
'#title' => $this->t('Maximum height'),
'#title_display' => 'invisible',
'#default_value' => $max_resolution[1],
'#min' => 1,
'#field_suffix' => ' ' . $this->t('pixels'),
'#suffix' => '</div>',
];
$min_resolution = explode('x', $settings['min_resolution']) + [
'',
'',
];
$element['min_resolution'] = [
'#type' => 'item',
'#title' => $this->t('Minimum image dimensions'),
'#element_validate' => [
[
static::class,
'validateResolution',
],
],
'#weight' => 4.2,
'#description' => $this->t('The minimum allowed image size expressed as WIDTH×HEIGHT (e.g. 640×480). Leave blank for no restriction. If a smaller image is uploaded, it will be rejected.'),
];
$element['min_resolution']['x'] = [
'#type' => 'number',
'#title' => $this->t('Minimum width'),
'#title_display' => 'invisible',
'#default_value' => $min_resolution[0],
'#min' => 1,
'#field_suffix' => ' × ',
'#prefix' => '<div class="form--inline clearfix">',
];
$element['min_resolution']['y'] = [
'#type' => 'number',
'#title' => $this->t('Minimum height'),
'#title_display' => 'invisible',
'#default_value' => $min_resolution[1],
'#min' => 1,
'#field_suffix' => ' ' . $this->t('pixels'),
'#suffix' => '</div>',
];
// Remove the description option.
unset($element['description_field']);
// Add title and alt configuration options.
$element['alt_field'] = [
'#type' => 'checkbox',
'#title' => $this->t('Enable <em>Alt</em> field'),
'#default_value' => $settings['alt_field'],
'#description' => $this->t('Short description of the image used by screen readers and displayed when the image is not loaded. Enabling this field is recommended.'),
'#weight' => 9,
];
$element['alt_field_required'] = [
'#type' => 'checkbox',
'#title' => $this->t('<em>Alt</em> field required'),
'#default_value' => $settings['alt_field_required'],
'#description' => $this->t('Making this field required is recommended.'),
'#weight' => 10,
'#states' => [
'visible' => [
':input[name="settings[alt_field]"]' => [
'checked' => TRUE,
],
],
],
];
$element['title_field'] = [
'#type' => 'checkbox',
'#title' => $this->t('Enable <em>Title</em> field'),
'#default_value' => $settings['title_field'],
'#description' => $this->t('The title attribute is used as a tooltip when the mouse hovers over the image. Enabling this field is not recommended as it can cause problems with screen readers.'),
'#weight' => 11,
];
$element['title_field_required'] = [
'#type' => 'checkbox',
'#title' => $this->t('<em>Title</em> field required'),
'#default_value' => $settings['title_field_required'],
'#weight' => 12,
'#states' => [
'visible' => [
':input[name="settings[title_field]"]' => [
'checked' => TRUE,
],
],
],
];
// Add default_image element.
static::defaultImageForm($element, $settings);
$element['default_image']['#description'] = $this->t("If no image is uploaded, this image will be shown on display and will override the field's default image.");
return $element;
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.