1 filter.pages.inc filter_format_editor_image_form($form, &$form_state, $format)

Form callback: Display a form for inserting/editing an image.

File

core/modules/filter/filter.pages.inc, line 35
User page callbacks for the Filter module.

Code

function filter_format_editor_image_form($form, &$form_state, $format) {
  $form_state['format'] = $format;

  // Record the dialog selector that needs to be closed if present.
  if (isset($form_state['input']['dialogOptions']['target'])) {
    $form_state['storage']['dialog_selector'] = $form_state['input']['dialogOptions']['target'];
  }

  // Record if image uploading is requested by the calling element.
  $element_supports_uploads = !isset($form_state['input']['dialogOptions']['uploads']) || (bool) $form_state['input']['dialogOptions']['uploads'];

  // Populate defaults.
  $values = array(
    'src' => '',
    'data-file-id' => NULL,
    'alt' => '',
    'width' => '',
    'height' => '',
    'data-align' => 'none',
    'data-has-caption' => 'false',
  );
  // Pull in any default values set by the editor.
  if (isset($form_state['input']['editor_object'])) {
    $values = array_merge($values, $form_state['input']['editor_object']);
  }
  // Store original values for reference.
  $values['previous_width'] = $values['width'];
  $values['previous_height'] = $values['height'];

  // Determine the style from path - this does not work with SVG, as those get
  // always served from their original path.
  $image_style_default = NULL;
  if (!empty($values['src']) && strpos($values['src'], 'styles/') > 0) {
    $path_parts = explode('/', $values['src']);
    $before_style = array_search('styles', $path_parts);
    $image_style_default = $path_parts[$before_style + 1];
  }

  // If an image style is set, remove the width and height, which do not apply
  // to image styles (they reset on save). This also lets images take on their
  // original dimensions if setting back to the original image.
  if (isset($image_style_default)) {
    $values['width'] = '';
    $values['height'] = '';
  }

  // Set the dialog title and submit button label.
  if (!empty($values['src']) || !empty($form_state['storage']['editing_image'])) {
    // Remember this is editing, not inserting. In the event the image is
    // removed and re-uploaded.
    $form_state['storage']['editing_image'] = TRUE;
    backdrop_set_title(t('Edit image details'));
    $submit_button_label = t('Update');
  }
  else {
    backdrop_set_title(t('Insert image'));
    $submit_button_label = t('Insert');
  }

  // Construct strings to use in the upload validators.
  $upload_settings = isset($format->editor_settings['image_upload']) ? $format->editor_settings['image_upload'] : array();
  $upload_settings += array(
    'status' => 0,
    'dimensions' => array('max_width' => '', 'max_height' => ''),
    'max_size' => NULL,
    'scheme' => 'public',
    'alt_required' => FALSE,
    'directory' => 'inline-images',
  );

  if (!empty($upload_settings['max_dimensions']['width']) || !empty($upload_settings['max_dimensions']['height'])) {
    $max_dimensions = $upload_settings['max_dimensions']['width'] . 'x' . $upload_settings['max_dimensions']['height'];
  }
  else {
    $max_dimensions = 0;
  }
  $max_filesize = !empty($upload_settings['max_size']) ? min(parse_size($upload_settings['max_size']), file_upload_max_size()) : file_upload_max_size();
  $orientation = !empty($upload_settings['orientate']) ? $upload_settings['orientate'] : 0;
  $existing_file = !empty($values['data-file-id']) ? file_load($values['data-file-id']) : NULL;
  $fid = $existing_file ? $existing_file->fid : NULL;
  $form['image']['fid'] = array(
    '#title' => t('Upload an image'),
    '#type' => 'managed_file',
    '#upload_location' => $upload_settings['scheme'] . '://' . $upload_settings['directory'],
    '#default_value' => $fid ? $fid : NULL,
    '#upload_validators' => array(
      'file_validate_extensions' => array(implode(' ', image_get_supported_extensions())),
      'file_validate_image_orientation' => array($orientation),
      'file_validate_size' => array($max_filesize),
      'file_validate_image_resolution' => array($max_dimensions),
    ),
    '#wrapper_attributes' => array(
      'data-editor-image-toggle' => t('Upload an image'),
    ),
    '#parents' => array('fid'),
    '#weight' => -10,
    '#access' => $element_supports_uploads && $upload_settings['status'] && user_access('upload editor images'),
  );

  // Use a "textfield" rather than "url" to allow relative paths.
  $form['image']['src'] = array(
    '#title' => t('Select from library'),
    '#type' => 'textfield',
    '#element_validate' => array('_filter_format_editor_link_url_validate'),
    '#placeholder' => '/example/image.jpg',
    '#default_value' => $values['src'],
    '#parents' => array('attributes', 'src'),
    '#wrapper_attributes' => array(
      'data-editor-image-toggle' => t('Select from library'),
    ),
    '#weight' => -8,
    '#maxlength' => 2048,
  );

  // If no current value or an existing file exists, default to showing
  // the uploading interface.
  if ($fid || empty($form['image']['src']['#default_value'])) {
    $form['image']['src']['#default_value'] = '';
  }

  $form['alt'] = array(
    '#title' => t('Alternative text'),
    '#type' => 'textfield',
    '#default_value' => $values['alt'],
    '#description' => t('The alt attribute may be used by screen readers, search engines, and when the image cannot be loaded.'),
    '#parents' => array('attributes', 'alt'),
    '#required' => $upload_settings['alt_required'],
  );

  $use_image_styles = !empty($format->editor_settings['image_styles']['status']) && module_exists('image');

  $available_style_options = array();
  if ($use_image_styles) {
    $all_image_styles = image_style_options(FALSE, PASS_THROUGH);
    foreach ($format->editor_settings['image_styles']['list'] as $key => $value) {
      if ($key === '' && $value !== 0) {
        $available_style_options[$key] = t('None (original image)');
      }
      elseif (!empty($value) && array_key_exists($key, $all_image_styles)) {
        $available_style_options[$key] = $all_image_styles[$key];
      }
    }
  }
  // If the image style list is empty (the image styles may no longer exist),
  // fallback to the normal no image style behavior.
  $use_image_styles = !empty($available_style_options);
  $form['image_style'] = array(
    '#title' => t('Image style'),
    '#type' => 'select',
    '#options' => $available_style_options,
    '#default_value' => $image_style_default,
    '#access' => $use_image_styles,
  );

  $form['size'] = array(
    '#title' => t('Image size'),
    '#wrapper_attributes' => array('class' => array('editor-image-size')),
    '#type' => 'item',
    '#states' => array(
      'visible' => array(
        ':input[name="image_style"]' => array('value' => ''),
      ),
    ),
  );
  $form['size']['width'] = array(
    '#title' => t('Width'),
    '#title_display' => 'attribute',
    '#type' => 'number',
    '#default_value' => $values['width'],
    '#max' => 99999,
    '#attributes' => array('placeholder' => t('width')),
    '#parents' => array('attributes', 'width'),
    '#field_suffix' => ' × ',
  );
  $form['size']['height'] = array(
    '#title' => t('Height'),
    '#title_display' => 'attribute',
    '#type' => 'number',
    '#default_value' => $values['height'],
    '#max' => 99999,
    '#attributes' => array('placeholder' => t('height')),
    '#parents' => array('attributes', 'height'),
    '#field_suffix' => ' ' . t('pixels'),
  );
  // Original values used for reference in submit handler.
  $form['size']['previous_width'] = array(
    '#type' => 'value',
    '#value' => $values['previous_width'],
  );
  $form['size']['previous_height'] = array(
    '#type' => 'value',
    '#value' => $values['previous_height'],
  );

  $form['align'] = array(
    '#title' => t('Align'),
    '#type' => 'radios',
    '#default_value' => $values['data-align'],
    '#options' => array(
      'none' => t('None'),
      'left' => t('Left'),
      'center' => t('Center'),
      'right' => t('Right'),
    ),
    '#wrapper_attributes' => array('class' => array('editor-image-align')),
    '#parents' => array('attributes', 'data-align'),
    '#access' => !empty($format->filters['filter_image_align']->status),
  );
  $form['caption'] = array(
    '#title' => t('Add a caption'),
    '#type' => 'checkbox',
    '#default_value' => (strcmp($values['data-has-caption'], 'false') !== 0) ? (bool) $values['data-has-caption'] : FALSE,
    '#parents' => array('attributes', 'data-has-caption'),
    '#access' => !empty($format->filters['filter_image_caption']->status),
  );

  // This button is hidden from display and clicked via JavaScript in
  // Backdrop.behaviors.editorImageDialog in filter.js.
  $form['library_open'] = array(
    '#type' => 'submit',
    '#value' => t('Select from library'),
    '#name' => 'library_open',
    '#attributes' => array(
      'class' => array('js-hide'),
    ),
    '#ajax' => array(
      'callback' => '_filter_image_library_ajax',
      'event' => 'click',
    ),
    '#limit_validation_errors' => array(),
    '#validate' => array(),
    '#submit' => array(),
  );

  $form['actions']['#type'] = 'actions';
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => $submit_button_label,
    '#ajax' => array(
      'callback' => 'filter_format_editor_dialog_save',
      'event' => 'click',
    ),
  );

  return $form;
}