1 filter.module filter_formats($account = NULL)

Retrieves a list of text formats, ordered by weight.

Parameters

User $account: (optional) If provided, only those formats that are allowed for this user account will be returned. All formats will be returned otherwise. Defaults to NULL.

Return value

stdClass[]: An array of text format objects, keyed by the format ID and ordered by weight.

See also

filter_formats_reset()

File

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

Code

function filter_formats($account = NULL) {
  global $language;
  $formats = &backdrop_static(__FUNCTION__, array());

  // All available formats are cached for performance.
  if (!isset($formats['all'])) {
    if ($cache = cache()->get("filter_formats:{$language->langcode}")) {
      $formats['all'] = $cache->data;
    }
    else {
      $formats['all'] = array();

      $all_filter_info = filter_get_filters();
      $all_editor_info = filter_get_editors();
      $config_names = config_get_names_with_prefix('filter.format.');
      $filter_formats = config_load_multiple($config_names);
      backdrop_sort($filter_formats, array('weight' => SORT_NUMERIC, 'name' => SORT_STRING));
      foreach ($filter_formats as $format_name => $filter_format) {
        $filter_format += array(
          'status' => 1,
          'weight' => 0,
          'editor' => NULL,
          'editor_settings' => array(),
        );
        $filter_format = (object) $filter_format;

        // We don't include disabled formats.
        if (!$filter_format->status) {
          continue;
        }

        // Sort the filters by weight.
        backdrop_sort($filter_format->filters);

        // Populate defaults for the filters and cast to an object.
        foreach ($filter_format->filters as $filter_name => $filter) {
          $filter += array(
            'name' => $filter_name,
            'status' => 1,
            'weight' => 0,
            'settings' => array(),
          );
          if (isset($all_filter_info[$filter_name]['default settings'])) {
            $filter['settings'] += $all_filter_info[$filter_name]['default settings'];
          }
          $filter_format->filters[$filter_name] = (object) $filter;
        }
        // Populate defaults for the editor associated with this format.
        if (isset($all_editor_info[$filter_format->editor]['default settings'])) {
          $filter_format->editor_settings += $all_editor_info[$filter_format->editor]['default settings'];
        }

        $formats['all'][$filter_format->format] = $filter_format;
      }

      cache()->set("filter_formats:{$language->langcode}", $formats['all']);
    }
  }

  // Build a list of user-specific formats.
  if (isset($account) && !isset($formats['user'][$account->uid])) {
    $formats['user'][$account->uid] = array();
    foreach ($formats['all'] as $format) {
      if (filter_access($format, $account)) {
        $formats['user'][$account->uid][$format->format] = $format;
      }
    }
  }

  return isset($account) ? $formats['user'][$account->uid] : $formats['all'];
}