filter_format_save

Versions
7
filter_format_save(&$format)

Save a text format object to the database.

Parameters

$format A format object using the properties:

  • 'name': The title of the text format.
  • 'format': (optional) The internal ID of the text format. If omitted, a new text format is created.
  • 'roles': (optional) An associative array containing the roles allowed to access/use the text format.
  • 'filters': (optional) An associative, multi-dimensional array of filters assigned to the text format, using the properties:

    • 'weight': The weight of the filter in the text format.
    • 'status': A boolean indicating whether the filter is enabled in the text format.
    • 'module': The name of the module implementing the filter.
    • 'settings': (optional) An array of configured settings for the filter. See hook_filter_info() for details.

▾ 2 functions call filter_format_save()

filter_admin_format_form_submit in modules/filter/filter.admin.inc
Process text format form submissions.
filter_update_7005 in modules/filter/filter.install
Integrate text formats with the user permissions system.

Code

modules/filter/filter.module, line 182

<?php
function filter_format_save(&$format) {
  $format->name = trim($format->name);

  // Add a new text format.
  if (empty($format->format)) {
    $return = drupal_write_record('filter_format', $format);
  }
  else {
    $return = drupal_write_record('filter_format', $format, 'format');
  }

  // Get the current filters in the format, to add new filters to the bottom.
  $current = filter_list_format($format->format);
  $filter_info = filter_get_filters();
  if (!isset($format->filters)) {
    $format->filters = array();
  }
  foreach ($format->filters as $name => $filter) {
    $fields = array();
    // Add new filters to the bottom.
    $fields['weight'] = isset($current[$name]->weight) ? $current[$name]->weight : 10;
    $fields['status'] = $filter['status'];
    $fields['module'] = $filter_info[$name]['module'];
    $format->filters[$name]['module'] = $filter_info[$name]['module'];
    // Only update settings if there are any.
    if (!empty($filter['settings'])) {
      $fields['settings'] = serialize($filter['settings']);
    }
    db_merge('filter')
      ->key(array(
        'format' => $format->format,
        'name' => $name,
      ))
      ->fields($fields)
      ->execute();
  }

  if ($return == SAVED_NEW) {
    module_invoke_all('filter_format_insert', $format);
  }
  else {
    module_invoke_all('filter_format_update', $format);
    // Explicitly indicate that the format was updated. We need to do this
    // since if the filters were updated but the format object itself was not,
    // the call to drupal_write_record() above would not return an indication
    // that anything had changed.
    $return = SAVED_UPDATED;

    // Clear the filter cache whenever a text format is updated.
    cache_clear_all($format->format . ':', 'cache_filter', TRUE);
  }

  filter_formats_reset();

  return $return;
}
?>
Login or register to post comments
 
 

All source code and documentation on this site is released under the terms of the GNU General Public License, version 2 and later. Drupal is a registered trademark of Dries Buytaert.