1 filter.pages.inc | filter_format_editor_link_form($form, &$form_state, $format) |
Form callback: Display a form for inserting/editing a link or uploading a file.
File
- core/
modules/ filter/ filter.pages.inc, line 260 - User page callbacks for the Filter module.
Code
function filter_format_editor_link_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 file uploading is requested by the calling element.
$element_supports_uploads = !isset($form_state['input']['dialogOptions']['uploads']) || (bool) $form_state['input']['dialogOptions']['uploads'];
// Pull in any default values set by the editor.
$values = array();
if (isset($form_state['input']['editor_object'])) {
$values = $form_state['input']['editor_object'];
}
// Set the dialog title.
if (!empty($values['href'])) {
backdrop_set_title(t('Edit link'));
}
else {
backdrop_set_title(t('Insert link'));
}
// Use a "textfield" rather than "url" to allow relative paths.
$form['href'] = array(
'#title' => t('Link destination'),
'#type' => 'textfield',
'#element_validate' => array('_filter_format_editor_link_url_validate'),
'#placeholder' => ' ',
'#default_value' => isset($values['href']) ? $values['href'] : NULL,
'#parents' => array('attributes', 'href'),
'#wrapper_attributes' => array(
'data-editor-image-toggle' => t('Link to existing content'),
),
'#weight' => -10,
'#maxlength' => 256,
'#autocomplete_path' => 'path-autocomplete',
'#description' => t('Start typing to see a list of suggestions, enter the relative path to another page, or enter an external URL.<br>Examples: <em>About Us</em>, <em>/about</em>, or <em>http://backdropcms.org</em>'),
);
// Construct strings to use in the upload validators.
$upload_settings = isset($format->editor_settings['file_upload']) ? $format->editor_settings['file_upload'] : array();
$upload_settings += array(
'max_size' => '',
'file_extensions' => 'txt',
'directory' => 'inline-files',
'data-file-id' => NULL,
'status' => 0,
'scheme' => 'public',
);
$max_filesize = !empty($upload_settings['max_size']) ? min(parse_size($upload_settings['max_size']), file_upload_max_size()) : file_upload_max_size();
$file_extensions = $upload_settings['file_extensions'];
$upload_validators = array(
'file_validate_extensions' => array($file_extensions),
'file_validate_size' => array($max_filesize),
);
$file_directory = $upload_settings['directory'];
$existing_file = !empty($values['data-file-id']) ? file_load($values['data-file-id']) : NULL;
$fid = $existing_file ? $existing_file->fid : NULL;
$form['file']['fid'] = array(
'#title' => t('Upload a file and link to it'),
'#type' => 'managed_file',
'#upload_location' => $upload_settings['scheme'] . '://' . $file_directory,
'#default_value' => $fid ? $fid : NULL,
'#upload_validators' => $upload_validators,
'#wrapper_attributes' => array(
'data-editor-image-toggle' => t('Upload a file and link to it'),
),
'#parents' => array('fid'),
'#weight' => -2,
'#access' => $element_supports_uploads && $upload_settings['status'] && user_access('upload editor files'),
'#description' => theme('file_upload_help', array('upload_validators' => $upload_validators)),
);
// If no current value or an existing file exists, default to showing
// the uploading interface.
if ($fid || empty($form['file']['src']['#default_value'])) {
$form['file']['fid']['#weight'] = -10;
$form['file']['src']['#default_value'] = '';
}
// Otherwise if editing an unmanaged file, show the raw URL field.
else {
$form['file']['src']['#weight'] = -10;
}
$form['text'] = array(
'#title' => t('Link text'),
'#type' => 'textfield',
'#default_value' => isset($values['text']) ? $values['text'] : '',
'#parents' => array('attributes', 'text'),
);
$form['more'] = array(
'#title' => t('More link options'),
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['more']['target'] = array(
'#title' => t('Open the link destination in a new window'),
'#type' => 'checkbox',
'#return_value' => '_blank',
'#default_value' => isset($values['target']) ? $values['target'] : FALSE,
'#parents' => array('attributes', 'target'),
);
$form['more']['class'] = array(
'#title' => t('CSS classes'),
'#description' => t('Separate multiple CSS classes with spaces.'),
'#type' => 'textfield',
'#default_value' => isset($values['class']) ? $values['class'] : '',
'#parents' => array('attributes', 'class'),
);
$fragment_info_link = l(t('fragment'), 'https://en.wikipedia.org/wiki/Fragment_identifier', array('attributes' => array('target' => '_blank')));
$form['more']['id'] = array(
'#title' => t('Unique Identifier'),
'#description' => t('Setting an ID allows linking to this part of the page using a URL !fragment.', array('!fragment' => $fragment_info_link)),
'#type' => 'textfield',
'#default_value' => isset($values['id']) ? $values['id'] : '',
'#parents' => array('attributes', 'id'),
);
$form['more']['title'] = array(
'#title' => t('Link title'),
'#description' => t('The title attribute of the link. Usually shown as a small tooltip on hover.'),
'#type' => 'textfield',
'#default_value' => isset($values['title']) ? $values['title'] : '',
'#parents' => array('attributes', 'title'),
);
$form['more']['rel'] = array(
'#title' => t('Relationship (rel)'),
'#description' => t('Specifies the relationship between the current page and the linked page.'),
'#type' => 'textfield',
'#default_value' => isset($values['rel']) ? $values['rel'] : '',
'#parents' => array('attributes', 'rel'),
);
$form['actions']['#type'] = 'actions';
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
'#ajax' => array(
'callback' => 'filter_format_editor_dialog_save',
'event' => 'click',
),
);
return $form;
}