diff --git a/includes/ckeditor.admin.inc b/includes/ckeditor.admin.inc index 259d59c..c2176e3 100644 --- a/includes/ckeditor.admin.inc +++ b/includes/ckeditor.admin.inc @@ -672,7 +672,28 @@ function ckeditor_admin_profile_form($form, $form_state, $task, $profile = NULL) '#title' => t('Text formats'), '#default_value' => !empty($profile->input_formats) ? array_keys((array) $profile->input_formats) : array(), '#options' => $formats, - '#description' => t('Choose the text formats where you want to load CKEditor.') + '#description' => t('Choose the text formats where you want to load CKEditor.'), + '#states' => array( + // Disable the checkboxes if there are any field ID's entered. + 'disabled' => array( + 'textarea[name="fields"]' => array('filled' => TRUE), + ), + ), + ); + + $form['basic']['fields'] = array( + '#title' => t('Fields'), + '#type' => 'textarea', + '#description' => t('Restrict this profile to the fields with the given ID\'s instead of text formats. Enter one field ID per line. This works for fields created with the Field API (e.g. in node forms) that have a text format. To determine the ID of a textarea, right-click into it, click "inspect element" or the like and then copy the ID in id="..." without the quotes into this field.'), + '#default_value' => !empty($form['_profile']['#value']->settings['fields']) + ? $form['_profile']['#value']->settings['fields'] + : '', + '#states' => array( + // Disable the textarea if any input format has been chosen. + 'disabled' => array( + 'input[name^="input_formats"]' => array('checked' => TRUE), + ), + ), ); $form['security'] = array( @@ -1519,6 +1540,24 @@ function ckeditor_admin_profile_form_submit($form, &$form_state) { drupal_set_message(t('Your CKEditor profile was created.')); } + // If after clearing the fields textarea an input format has been checked, + // disabling the textarea via #states, $edit['fields'] contains the textarea's + // default value as previously saved. This is because disabled fields are not + // present in $_POST and so Drupal merges the default value in + // drupal_form_submit(). As this would save the actually removed field + // configuration again, we clear it here. + if (!isset($form_state['input']['fields'])) { + $edit['fields'] = ''; + } + + // If the profile is configured for fields, assign a fake input format, using + // the profile's name with a prepended underscore, so this profile can be + // loaded. + if (!empty($edit['fields'])) { + $format_key = '_' . $edit['name']; + $edit['input_formats'][$format_key] = $format_key; + } + $settings = ckeditor_admin_values_to_settings($edit); db_insert('ckeditor_settings') ->fields(array( diff --git a/includes/ckeditor.lib.inc b/includes/ckeditor.lib.inc index 892e663..809fae3 100644 --- a/includes/ckeditor.lib.inc +++ b/includes/ckeditor.lib.inc @@ -690,6 +690,33 @@ function ckeditor_get_profile($input_format) { } /** + * Get all CKEditor profiles (excluding the global one). + * + * @return + * Associative array of profile objects, keyed and ordered by profile name. + */ +function ckeditor_get_profiles() { + static $profiles = NULL; + + if (is_null($profiles)) { + $query = db_select('ckeditor_settings', 's') + ->fields('s', array('name')) + ->condition('s.name', 'CKEditor Global Profile', '<>') + ->orderby('name') + ->execute(); + + $profiles = array(); + while($name = $query->fetchField()) { + $profiles[$name] = ckeditor_profile_load($name); + } + } + + return is_array($profiles) && count($profiles) + ? $profiles + : FALSE; +} + +/** * Return CKEditor profile list */ function ckeditor_profile_input_formats() { @@ -1406,7 +1433,38 @@ function ckeditor_load_by_field($field, $format, $show_toggle = TRUE, $add_field } $global_profile = ckeditor_profile_load('CKEditor Global Profile'); + + // First check if there is a profile for the given format. $profile = ckeditor_get_profile($format); + + // If there is no profile for the given format. + if (!$profile) { + foreach (ckeditor_get_profiles() as $p) { + // If the current profile is configured for specific fields. + if (!empty($p->settings['fields'])) { + // Make an array of the field names. + $fields = preg_split("/\r\n|\n|\r/", $p->settings['fields']); + + // If the field is set for the current profile. + if (in_array($field['#id'], $fields)) { + $profile = $p; + + // Set the fake input format's name as CKEditor will load based on it + // in the browser + $format = '_' . $p->name; + + break; + } + } + } + } + + // If there is no profile, neither for the format nor for the field, don't + // load CKEditor. + if (!$profile) { + return $field; + } + $host = base_path(); if ($profile === FALSE) {