function editor_editor_presave

Implements hook_ENTITY_TYPE_presave().

File

core/modules/editor/editor.module, line 653

Code

function editor_editor_presave(EditorInterface $editor) {
  // @see editor_post_update_sanitize_image_upload_settings()
  $image_upload_settings = $editor->getImageUploadSettings();
  // When image uploads are disabled, then none of the other key-value pairs
  // make sense.
  // TRICKY: the configuration system has historically stored `type: boolean`
  // not as `true` and `false`, but as `1` and `0`, so use `==`, not `===`.
  // @see editor_post_update_sanitize_image_upload_settings()
  if (!array_key_exists('status', $image_upload_settings) || $image_upload_settings['status'] == FALSE) {
    $editor->setImageUploadSettings([
      'status' => FALSE,
    ]);
  }
  else {
    // When image uploads are enabled, then some of the key-value pairs need
    // some conversions to comply with the config schema. Note that all these
    // keys SHOULD exist, but because validation has historically been absent,
    // err on the side of caution.
    // @see editor_post_update_sanitize_image_upload_settings()
    if (array_key_exists('directory', $image_upload_settings) && $image_upload_settings['directory'] === '') {
      $image_upload_settings['directory'] = NULL;
    }
    if (array_key_exists('max_size', $image_upload_settings) && $image_upload_settings['max_size'] === '') {
      $image_upload_settings['max_size'] = NULL;
    }
    if (array_key_exists('max_dimensions', $image_upload_settings)) {
      if (!array_key_exists('width', $image_upload_settings['max_dimensions']) || $image_upload_settings['max_dimensions']['width'] === 0) {
        $image_upload_settings['max_dimensions']['width'] = NULL;
      }
      if (!array_key_exists('height', $image_upload_settings['max_dimensions']) || $image_upload_settings['max_dimensions']['height'] === 0) {
        $image_upload_settings['max_dimensions']['height'] = NULL;
      }
    }
    $editor->setImageUploadSettings($image_upload_settings);
  }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.