1 filter.module filter_format_build_format($format)

Builds a text format object from initial values.

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

$format: A text format object suitable for saving to configuration.

File

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

Code

function filter_format_build_format($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;
  }

  return $format;
}