Same name and namespace in other branches
  1. 4.7.x modules/filter.module \filter_admin_format_form_submit()
  2. 5.x modules/filter/filter.module \filter_admin_format_form_submit()
  3. 7.x modules/filter/filter.admin.inc \filter_admin_format_form_submit()

Process filter format form submissions.

File

modules/filter/filter.admin.inc, line 175
Admin page callbacks for the filter module.

Code

function filter_admin_format_form_submit($form, &$form_state) {
  $format = isset($form_state['values']['format']) ? $form_state['values']['format'] : NULL;
  $current = filter_list_format($format);
  $name = trim($form_state['values']['name']);
  $cache = TRUE;

  // Add a new filter format.
  if (!$format) {
    $new = TRUE;
    db_query("INSERT INTO {filter_formats} (name) VALUES ('%s')", $name);
    $format = db_result(db_query("SELECT MAX(format) AS format FROM {filter_formats}"));
    drupal_set_message(t('Added input format %format.', array(
      '%format' => $name,
    )));
  }
  else {
    drupal_set_message(t('The input format settings have been updated.'));
  }
  db_query("DELETE FROM {filters} WHERE format = %d", $format);
  foreach ($form_state['values']['filters'] as $id => $checked) {
    if ($checked) {
      list($module, $delta) = explode('/', $id);

      // Add new filters to the bottom.
      $weight = isset($current[$id]->weight) ? $current[$id]->weight : 10;
      db_query("INSERT INTO {filters} (format, module, delta, weight) VALUES (%d, '%s', %d, %d)", $format, $module, $delta, $weight);

      // Check if there are any 'no cache' filters.
      $cache &= !module_invoke($module, 'filter', 'no cache', $delta);
    }
  }

  // We store the roles as a string for ease of use.
  // We should always set all roles to TRUE when saving a default role.
  // We use leading and trailing comma's to allow easy substring matching.
  $roles = array();
  if (isset($form_state['values']['roles'])) {
    foreach ($form_state['values']['roles'] as $id => $checked) {
      if ($checked) {
        $roles[] = $id;
      }
    }
  }
  if (!empty($form_state['values']['default_format'])) {
    $roles = ',' . implode(',', array_keys(user_roles())) . ',';
  }
  else {
    $roles = ',' . implode(',', $roles) . ',';
  }
  db_query("UPDATE {filter_formats} SET cache = %d, name='%s', roles = '%s' WHERE format = %d", $cache, $name, $roles, $format);
  cache_clear_all($format . ':', 'cache_filter', TRUE);

  // 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/filters';
  if (!empty($new)) {
    $return .= '/' . $format;
  }
  $form_state['redirect'] = $return;
  return;
}