Commit 6f41d692 authored by Dries's avatar Dries

- Patch #559658 by sun, dropcube: store filter settings per format, general...

- Patch #559658 by sun, dropcube: store filter settings per format, general API clean-up and documentation improvements.
parent 0963bac7
......@@ -79,6 +79,7 @@ Drupal 7.0, xxxx-xx-xx (development version)
contributed date or event modules installed, user time zone settings will
fallback to the system time zone and will have to be reconfigured by each user.
- Filter system:
* Revamped the filter API and text format storage.
* Refactored the HTML corrector to take advantage of PHP 5 features.
- Removed ping module:
* Contributed modules with similar functionality are available.
......
......@@ -144,7 +144,7 @@ function filter_admin_format_form(&$form_state, $format) {
}
// Table with filters
$filter_info = filter_get_filters();
$enabled = filter_list_format($format->format);
$filters = filter_list_format($format->format);
$form['filters'] = array('#type' => 'fieldset',
'#title' => t('Filters'),
......@@ -155,7 +155,7 @@ function filter_admin_format_form(&$form_state, $format) {
$form['filters'][$name] = array(
'#type' => 'checkbox',
'#title' => $filter['title'],
'#default_value' => isset($enabled[$name]),
'#default_value' => isset($filters[$name]),
'#description' => $filter['description'],
);
}
......@@ -201,22 +201,15 @@ function filter_admin_format_form_submit($form, &$form_state) {
$format->format = isset($form_state['values']['format']) ? $form_state['values']['format'] : NULL;
$status = filter_format_save($format);
// If a new filter was added, return to the main list of filters.
// Otherwise, stay on edit filter page to show new changes.
$return = 'admin/settings/formats';
switch ($status) {
case SAVED_NEW:
drupal_set_message(t('Added text format %format.', array('%format' => $format->name)));
$return .= '/' . $format->format;
break;
case SAVED_UPDATED:
drupal_set_message(t('The text format settings have been updated.'));
break;
}
$form_state['redirect'] = $return;
return;
}
/**
......@@ -259,7 +252,6 @@ function filter_admin_delete_submit($form, &$form_state) {
$form_state['redirect'] = 'admin/settings/formats';
}
/**
* Menu callback; display settings defined by a format's filters.
*/
......@@ -269,39 +261,66 @@ function filter_admin_configure_page($format) {
}
/**
* Build a form to change the settings for a format's filters.
* Build a form to change the settings for filters in a text format.
*
* The form is built by merging the results of 'settings callback' for each
* enabled filter in the given format.
*
* @ingroup forms
*/
function filter_admin_configure(&$form_state, $format) {
$list = filter_list_format($format->format);
$filters = filter_list_format($format->format);
$filter_info = filter_get_filters();
$form = array();
foreach ($list as $name => $filter) {
$form['#format'] = $format;
foreach ($filters as $name => $filter) {
if (isset($filter_info[$name]['settings callback']) && function_exists($filter_info[$name]['settings callback'])) {
$form_module = call_user_func($filter_info[$name]['settings callback'], $format->format);
}
if (isset($form_module) && is_array($form_module)) {
$form = array_merge($form, $form_module);
// Pass along stored filter settings and default settings, but also the
// format object and all filters to allow for complex implementations.
$defaults = (isset($filter_info[$name]['default settings']) ? $filter_info[$name]['default settings'] : array());
$settings_form = $filter_info[$name]['settings callback']($form_state, $filters[$name], $defaults, $format, $filters);
if (isset($settings_form) && is_array($settings_form)) {
$form['settings'][$name] = array(
'#type' => 'fieldset',
'#title' => check_plain($filter->title),
);
$form['settings'][$name] += $settings_form;
}
}
}
if (!empty($form)) {
$form = system_settings_form($form, TRUE);
}
else {
if (empty($form['settings'])) {
$form['error'] = array('#markup' => t('No settings are available.'));
return $form;
}
$form['format'] = array('#type' => 'hidden', '#value' => $format->format);
$form['#submit'][] = 'filter_admin_configure_submit';
$form['settings']['#tree'] = TRUE;
$form['submit'] = array('#type' => 'submit', '#value' => t('Save configuration'));
return $form;
}
/**
* Clear the filter's cache when configuration settings are saved.
* Form submit handler for text format filter configuration form.
*
* @see filter_admin_configure()
*/
function filter_admin_configure_submit($form, &$form_state) {
cache_clear_all($form_state['values']['format'] . ':', 'cache_filter', TRUE);
$format = $form['#format'];
foreach ($form_state['values']['settings'] as $name => $settings) {
db_update('filter')
->fields(array(
'settings' => serialize($settings),
))
->condition('format', $format->format)
->condition('name', $name)
->execute();
}
// Clear the filter's cache when configuration settings are saved.
cache_clear_all($format->format . ':', 'cache_filter', TRUE);
drupal_set_message(t('The configuration options have been saved.'));
}
/**
......@@ -366,10 +385,14 @@ function theme_filter_admin_order($form) {
*/
function filter_admin_order_submit($form, &$form_state) {
foreach ($form_state['values']['weights'] as $name => $weight) {
db_update('filter')
->fields(array('weight' => $weight))
->condition('format', $form_state['values']['format'])
->condition('name', $name)
db_merge('filter')
->key(array(
'format' => $form_state['values']['format'],
'name' => $name,
))
->fields(array(
'weight' => $weight,
))
->execute();
}
drupal_set_message(t('The filter ordering has been saved.'));
......
This diff is collapsed.
......@@ -13,11 +13,6 @@ function filter_schema() {
$schema['filter'] = array(
'description' => 'Table that maps filters (HTML corrector) to text formats (Filtered HTML).',
'fields' => array(
'fid' => array(
'type' => 'serial',
'not null' => TRUE,
'description' => 'Primary Key: Auto-incrementing filter ID.',
),
'format' => array(
'type' => 'int',
'not null' => TRUE,
......@@ -44,9 +39,22 @@ function filter_schema() {
'default' => 0,
'size' => 'tiny',
'description' => 'Weight of filter within format.',
)
),
'status' => array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'description' => 'Filter enabled status. (1 = enabled, 0 = disabled)',
),
'settings' => array(
'type' => 'text',
'not null' => FALSE,
'size' => 'big',
'serialize' => TRUE,
'description' => 'A serialized array of name value pairs that store the filter settings for the specific format.',
),
),
'primary key' => array('fid'),
'primary key' => array('format', 'name'),
'unique keys' => array(
'fmn' => array('format', 'module', 'name'),
),
......@@ -187,3 +195,68 @@ function filter_update_7003() {
return $ret;
}
/**
* Move filter settings storage into {filter} table.
*
* - Remove {filter}.fid.
* - Add (format, name) as primary key for {filter}.
* - Add {filter}.status.
* - Add {filter}.settings.
*/
function filter_update_7004() {
$ret = array();
db_drop_field($ret, 'filter', 'fid');
db_add_primary_key($ret, 'filter', array('format', 'name'));
db_add_field($ret, 'filter', 'status',
array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'description' => 'Filter enabled status. (1 = enabled, 0 = disabled)',
)
);
db_add_field($ret, 'filter', 'settings',
array(
'type' => 'text',
'not null' => FALSE,
'size' => 'big',
'serialize' => TRUE,
'description' => 'A serialized array of name value pairs that store the filter settings for the specific format.',
)
);
// Enable all existing filters ({filter} contained only enabled previously).
$ret[] = update_sql("UPDATE {filter} SET status = 1");
// Move filter settings from system variables into {filter}.settings.
$filters = db_query("SELECT * FROM {filter} WHERE module = :name", array(':name' => 'filter'));
foreach ($filters as $filter) {
$settings = array();
if ($filter->name == 'filter_html') {
if ($setting = variable_get("allowed_html_{$filter->format}", NULL)) {
$settings['allowed_html'] = $setting;
variable_del("allowed_html_{$filter->format}");
}
if ($setting = variable_get("filter_html_help_{$filter->format}", NULL)) {
$settings['filter_html_help'] = $setting;
variable_del("filter_html_help_{$filter->format}");
}
if ($setting = variable_get("filter_html_nofollow_{$filter->format}", NULL)) {
$settings['filter_html_nofollow'] = $setting;
variable_del("filter_html_nofollow_{$filter->format}");
}
}
elseif ($filter->name == 'filter_url') {
if ($setting = variable_get("filter_url_length_{$filter->format}", NULL)) {
$settings['filter_url_length'] = $setting;
variable_del("filter_url_length_{$filter->format}");
}
}
if (!empty($settings)) {
$ret[] = update_sql("UPDATE {filter} SET settings = '" . serialize($settings) . "' WHERE format = {$filter->format} AND name = '{$filter->name}'");
}
}
return $ret;
}
This diff is collapsed.
This diff is collapsed.
......@@ -31,6 +31,7 @@ function php_install() {
'module' => 'php',
'name' => 'php_code',
'weight' => 0,
'status' => 1,
))
->execute();
......
......@@ -79,7 +79,7 @@ function php_eval($code) {
/**
* Tips callback for php filter.
*/
function _php_filter_tips($format, $long = FALSE) {
function _php_filter_tips($filter, $format, $long = FALSE) {
global $base_url;
if ($long) {
$output = '<h4>' . t('Using custom PHP code') . '</h4>';
......
......@@ -10,20 +10,20 @@
* Implement hook_filter_format_insert().
*/
function filter_test_filter_format_insert($format) {
drupal_set_message(t('hook_filter_format_insert invoked.'));
drupal_set_message('hook_filter_format_insert invoked.');
}
/**
* Implement hook_filter_format_update().
*/
function filter_test_filter_format_update($format) {
drupal_set_message(t('hook_filter_format_update invoked.'));
drupal_set_message('hook_filter_format_update invoked.');
}
/**
* Implement hook_filter_format_delete().
*/
function filter_test_filter_format_delete($format, $default) {
drupal_set_message(t('hook_filter_format_delete invoked.'));
drupal_set_message('hook_filter_format_delete invoked.');
}
......@@ -425,13 +425,14 @@ function system_install() {
// Filtered HTML:
db_insert('filter')
->fields(array('format', 'module', 'name', 'weight'))
->fields(array('format', 'module', 'name', 'weight', 'status'))
// URL filter.
->values(array(
'format' => $filtered_html_format,
'module' => 'filter',
'name' => 'filter_url',
'weight' => 0,
'status' => 1,
))
// HTML filter.
->values(array(
......@@ -439,6 +440,7 @@ function system_install() {
'module' => 'filter',
'name' => 'filter_html',
'weight' => 1,
'status' => 1,
))
// Line break filter.
->values(array(
......@@ -446,6 +448,7 @@ function system_install() {
'module' => 'filter',
'name' => 'filter_autop',
'weight' => 2,
'status' => 1,
))
// HTML corrector filter.
->values(array(
......@@ -453,6 +456,7 @@ function system_install() {
'module' => 'filter',
'name' => 'filter_htmlcorrector',
'weight' => 10,
'status' => 1,
))
// Full HTML:
// URL filter.
......@@ -460,7 +464,8 @@ function system_install() {
'format' => $full_html_format,
'module' => 'filter',
'name' => 'filter_url',
'weight' => 0,
'weight' => 0,
'status' => 1,
))
// Line break filter.
->values(array(
......@@ -468,6 +473,7 @@ function system_install() {
'module' => 'filter',
'name' => 'filter_autop',
'weight' => 1,
'status' => 1,
))
// HTML corrector filter.
->values(array(
......@@ -475,6 +481,7 @@ function system_install() {
'module' => 'filter',
'name' => 'filter_htmlcorrector',
'weight' => 10,
'status' => 1,
))
->execute();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment