1 filter.module filter_format_save($format)

Saves a text format object to the database.

Parameters

$format: A format object having the properties:

  • format: A machine-readable name representing the ID of the text format to save. If this corresponds to an existing text format, that format will be updated; otherwise, a new format will be created.
  • name: The title of the text format.
  • status: (optional) An integer indicating whether the text format is enabled (1) or not (0). Defaults to 1.
  • weight: (optional) The weight of the text format, which controls its placement in text format lists. If omitted, the weight is set to 0.
  • filters: (optional) An associative, multi-dimensional array of filters assigned to the text format, keyed by the name of each filter and using the properties:

    • weight: (optional) The weight of the filter in the text format. If omitted, either the currently stored weight is retained (if there is one), or the filter is assigned a weight of 10, which will usually put it at the bottom of the list.
    • status: (optional) A boolean indicating whether the filter is enabled in the text format. If omitted, the filter will be disabled.
    • settings: (optional) An array of configured settings for the filter. See hook_filter_info() for details.

Return value

SAVED_NEW or SAVED_UPDATED.:

File

core/modules/filter/filter.module, line 280
Framework for handling the filtering of content.

Code

function filter_format_save($format) {
  $format->name = trim($format->name);
  $format->cache = _filter_format_is_cacheable($format);
  if (!isset($format->status)) {
    $format->status = 1;
  }
  if (!isset($format->weight)) {
    $format->weight = 0;
  }
  if (!isset($format->editor)) {
    $format->editor = NULL;
  }

  // Programmatic saves may not contain any filters.
  if (!isset($format->filters)) {
    $format->filters = array();
  }
  $all_filter_info = filter_get_filters();
  foreach ($format->filters as $name => $filter) {
    $filter_info = $all_filter_info[$name];

    // Ensure the $filter variable is an object. In Drupal 7, it was an array on
    // save, but an object on load. We always use an object in Backdrop for
    // consistency.
    $filter = (object) $filter;

    // If the format does not specify an explicit weight for a filter, assign
    // a default weight from hook_filter_info().
    $filter->weight = (int) (isset($filter->weight) ? $filter->weight : $filter_info['weight']);
    $filter->status = (int) (isset($filter->status) ? $filter->status : 0);
    $filter->module = $filter_info['module'];
    $filter->settings = isset($filter->settings) ? $filter->settings : array();
    $format->filters[$name] = $filter;
  }

  // Save the filter format.
  $config = config('filter.format.' . $format->format);
  $is_new = $config->isNew();

  // Convert the object to arrays for saving.
  $format_data = (array) $format;
  foreach ($format_data['filters'] as $filter_name => $filter) {
    $filter_data = (array) $filter;
    if (isset($filter_data['name'])) {
      unset($filter_data['name']);
    }
    $format_data['filters'][$filter_name] = $filter_data;
  }
  if (isset($format_data['is_new'])) {
    unset($format_data['is_new']);
  }

  $config->setData($format_data);
  $config->save();

  if ($is_new) {
    module_invoke_all('filter_format_insert', $format);
    $return = SAVED_NEW;
  }
  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 merge query above would not return an indication that anything had
    // changed.
    $return = SAVED_UPDATED;

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

  filter_formats_reset();

  return $return;
}