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

Submit handler for filter_format_editor_link_form().

File

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

Code

function filter_format_editor_link_form_submit($form, &$form_state) {
  // Look for file and get fid from uri.
  $fid = 0;
  $uri = '';
  if (isset($form_state['values']['attributes']['href']) && (strlen($form_state['values']['attributes']['href']) > 1)) {
    $uri = $form_state['values']['attributes']['href'];
    // Check for FID for this URI.
    $fid = _filter_get_file_id($uri);
  }
  // File upload will not have provided href but provides fid.
  elseif (isset($form_state['values']['fid'])) {
    $fid = $form_state['values']['fid'];
  }
  // If file is found get its URI.
  if ($fid > 0) {
    $file = file_load($fid);
    $uri = file_create_url($file->uri);
    // Try to make a local path if possible for better portability.
    // Absolute path is needed if Backdrop is installed in subdirectory.
    $absolute_path = parse_url($GLOBALS['base_url'], PHP_URL_PATH) . '/';

    $url = str_replace($GLOBALS['base_url'] . '/', $absolute_path, $uri);
    $form_state['values']['attributes']['data-file-id'] = $fid;
    $form_state['values']['attributes']['href'] = $url;
    unset($form_state['values']['fid']);

    // If no text was set, use the file name.
    if (strlen($form_state['values']['attributes']['text']) === 0) {
      $form_state['values']['attributes']['text'] = basename($url);
    }
  }
  else {
    $form_state['values']['attributes']['href'] = $uri;
    // Remove any reference to a data file.
    unset($form_state['values']['fid']);
    $form_state['values']['attributes']['data-file-id'] = NULL;
  }

  // Clean up optional attributes, if not specified, remove.
  $more = array('class', 'target', 'id', 'title', 'rel', 'data-file-id');
  foreach ($more as $attribute) {
    if (isset($form_state['values']['attributes'][$attribute])) {
      $value = trim($form_state['values']['attributes'][$attribute]);
      if (strlen($value) === 0 || ($attribute === 'target' && empty($value))) {
        unset($form_state['values']['attributes'][$attribute]);
      }
      else {
        $form_state['values']['attributes'][$attribute] = $value;
      }
    }
  }
}