You are here

function inline_responsive_images_form_editor_image_dialog_imagestyle_validate in Inline responsive images 8.2

Form validation handler for EditorImageDialog.

Ensures the image shown in the text editor matches the chosen image style.

See also

\Drupal\editor\Form\EditorImageDialog::buildForm()

\Drupal\editor\Form\EditorImageDialog::validateForm()

image_form_editor_image_dialog_alter()

1 string reference to 'inline_responsive_images_form_editor_image_dialog_imagestyle_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 205

Code

function inline_responsive_images_form_editor_image_dialog_imagestyle_validate(array &$form, FormStateInterface &$form_state) {
  $attributes =& $form_state
    ->getValue('attributes');
  if (!empty($form_state
    ->getValue('fid')[0])) {
    $image_style = \Drupal::entityTypeManager()
      ->getStorage('image_style')
      ->load($attributes['data-image-style']);
    $file = \Drupal\file\Entity\File::load($form_state
      ->getValue('fid')[0]);
    $uri = $file
      ->getFileUri();

    // 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'] = $image_style
      ->buildUrl($uri);

    // Set the 'width' and 'height' attributes to the image style's transformed
    // dimensions.
    $image = \Drupal::service('image.factory')
      ->get($uri);
    if ($image
      ->isValid()) {
      $dimensions = array(
        'width' => $image
          ->getWidth(),
        'height' => $image
          ->getHeight(),
      );
      $image_style
        ->transformDimensions($dimensions, $attributes['src']);
      $attributes['width'] = $dimensions['width'];
      $attributes['height'] = $dimensions['height'];
    }
  }
}