1 filter.module filter_formats($account = NULL, $include_disabled = FALSE)

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.

Boolean $include_disabled: (optional) If TRUE, will return all formats, including disabled formats.

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 750
Framework for handling the filtering of content.

Code

function filter_formats($account = NULL, $include_disabled = FALSE) {
  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;

        // 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 enabled formats based on the full list.
  if (!isset($formats['enabled'])) {
    foreach ($formats['all'] as $format) {
      if ($format->status) {
        $formats['enabled'][$format->format] = $format;
      }
    }
  }

  // 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)) {
        // Assemble a list of all formats (even disabled ones) for this user.
        $formats['user'][$account->uid]['all'][$format->format] = $format;
        // As well as a specific list of only enabled formats.
        if ($format->status) {
          $formats['user'][$account->uid]['enabled'][$format->format] = $format;
        }
      }
    }
  }

  $enabled_or_all = $include_disabled ? 'all' : 'enabled';
  if (isset($account)) {
    return $formats['user'][$account->uid][$enabled_or_all];
  }
  else {
    return $formats[$enabled_or_all];
  }
}