You are here

function editor_ckeditor_image_dialog_save in Editor 7

Form AJAX callback. Sends the save editor AJAX command and closes the dialog.

See also

filter_format_editor_link_form()

filter_format_editor_image_form()

1 string reference to 'editor_ckeditor_image_dialog_save'
editor_ckeditor_image_dialog_form in modules/editor_ckeditor/includes/editor_ckeditor.pages.inc
Form callback: Display a form for inserting/editing a link.

File

modules/editor_ckeditor/includes/editor_ckeditor.pages.inc, line 252
Page callbacks for the Editor CKEditor module.

Code

function editor_ckeditor_image_dialog_save($form, &$form_state) {
  $commands = array();

  // Convert any uploaded files from the FID values to data-entity-uuid
  // attributes and set data-entity-type to 'file'.
  $fid = isset($form_state['values']['fid']) ? $form_state['values']['fid'] : 0;
  if (!empty($fid)) {
    $file = file_load($fid);

    // Transform absolute image URLs to relative image URLs: prevent problems
    // on multisite set-ups and prevent mixed content errors.
    $scheme = file_uri_scheme($file->uri);
    if ($scheme && file_stream_wrapper_valid_scheme($scheme)) {
      $wrapper = file_stream_wrapper_get_instance_by_scheme($scheme);

      // Transform absolute image URLs to relative image URLs: prevent problems
      // on multisite set-ups and prevent mixed content errors.
      $file_url = base_path() . $wrapper
        ->getDirectoryPath() . '/' . file_uri_target($file->uri);
      $form_state['values']['attributes']['src'] = $file_url;
      $form_state['values']['attributes']['data-entity-id'] = $fid;
      $form_state['values']['attributes']['data-entity-type'] = 'file';
    }
  }

  // When the alt attribute is set to two double quotes, transform it to the
  // empty string: two double quotes signify "empty alt attribute". See above.
  if (trim($form_state['values']['attributes']['alt']) === '""') {
    $form_state['attributes']['alt'] = '';
  }
  if (form_get_errors()) {
    unset($form['#prefix'], $form['#suffix']);
    $status_messages = array(
      '#theme' => 'status_messages',
    );
    $output = drupal_render($form);
    $output = '<div>' . drupal_render($status_messages) . $output . '</div>';
    $commands[] = ajax_command_html('#editor-ckeditor-image-dialog-form', $output);
  }
  else {
    $commands[] = editor_command_editor_dialog_save($form_state['values']);
    $commands[] = dialog_command_close_modal_dialog();
  }
  return array(
    '#type' => 'ajax',
    '#commands' => $commands,
  );
}