function inline_responsive_images_form_editor_image_dialog_responsive_validate in Inline responsive images 8.2
1 string reference to 'inline_responsive_images_form_editor_image_dialog_responsive_validate'
- inline_responsive_images_form_editor_image_dialog_alter in ./
inline_responsive_images.module - Implements hook_form_FORM_ID_alter() for EditorImageDialog.
File
- ./
inline_responsive_images.module, line 232
Code
function inline_responsive_images_form_editor_image_dialog_responsive_validate(array &$form, FormStateInterface &$form_state) {
$attributes =& $form_state
->getValue('attributes');
if (!empty($form_state
->getValue('fid')[0])) {
$responsive_image_style = \Drupal::entityTypeManager()
->getStorage('responsive_image_style')
->load($attributes['data-responsive-image-style']);
$file = File::load($form_state
->getValue('fid')[0]);
$uri = $file
->getFileUri();
// Set up original file information.
$image = \Drupal::service('image.factory')
->get($uri);
if ($image
->isValid()) {
$dimensions = [
'width' => $image
->getWidth(),
'height' => $image
->getHeight(),
];
}
else {
// @todo: what if the image is not valid?
$dimensions = [
'width' => 1000,
'height' => 1000,
];
}
// Select the first (i.e. smallest) breakpoint and the 1x multiplier. We
// choose to show the image in the editor as if it were being viewed in the
// narrowest viewport, so that when the user starts to edit this content
// again on a mobile device, it will work fine.
$breakpoint_machine_names = array_keys($responsive_image_style
->getKeyedImageStyleMappings()['responsive_image.viewport_sizing']);
$image_style_mapping = $responsive_image_style
->getKeyedImageStyleMappings()['responsive_image.viewport_sizing'][$breakpoint_machine_names[0]];
switch ($image_style_mapping['image_mapping_type']) {
case 'sizes':
// More than one image style can be mapped. Use the smallest one.
$transformed_dimensions = $dimensions;
foreach ($image_style_mapping['image_mapping']['sizes_image_styles'] as $mapped_image_style) {
$new_dimensions = responsive_image_get_image_dimensions($mapped_image_style, $dimensions, $uri);
if (!$transformed_dimensions || $transformed_dimensions['width'] >= $new_dimensions['width']) {
$image_style_name = $mapped_image_style;
$transformed_dimensions = $new_dimensions;
}
}
break;
case 'image_style':
$image_style_name = $image_style_mapping['image_mapping'];
$transformed_dimensions = responsive_image_get_image_dimensions($image_style_name, $dimensions, $uri);
break;
}
// Set the 'src' attribute to the image style URL. FilterImageStyle will
// look at the 'data-editor-file-uuid' attribute, not the 'src' attribute to
// render the appropriate output.
$attributes['src'] = _responsive_image_image_style_url($image_style_name, $uri);
// Set the 'width' and 'height' attributes to the image style's transformed
// dimensions.
if ($image
->isValid()) {
$attributes['width'] = $transformed_dimensions['width'];
$attributes['height'] = $transformed_dimensions['height'];
}
}
}