function filter_formats

Same name and namespace in other branches
  1. 7.x modules/filter/filter.module \filter_formats()
  2. 9 core/modules/filter/filter.module \filter_formats()
  3. 8.9.x core/modules/filter/filter.module \filter_formats()
  4. 10 core/modules/filter/filter.module \filter_formats()

Retrieves a list of enabled text formats, ordered by weight.

Parameters

\Drupal\Core\Session\AccountInterface|null $account: (optional) If provided, only those formats that are allowed for this user account will be returned. All enabled formats will be returned otherwise. Defaults to NULL.

Return value

\Drupal\filter\FilterFormatInterface[] An array of text format objects, keyed by the format ID and ordered by weight.

See also

filter_formats_reset()

15 calls to filter_formats()
BreadcrumbTest::testBreadCrumbs in core/modules/system/tests/src/Functional/Menu/BreadcrumbTest.php
Tests breadcrumbs on node and administrative paths.
DefaultViewsTest::createTerm in core/modules/views/tests/src/Functional/DefaultViewsTest.php
Returns a new term with random properties in vocabulary $vid.
FilterCrudTest::testTextFormatCrud in core/modules/filter/tests/src/Kernel/FilterCrudTest.php
Tests CRUD operations for text formats and filters.
FilterFormat::getPossibleOptions in core/modules/filter/src/Plugin/DataType/FilterFormat.php
Returns an array of possible values with labels for display.
FilterFormat::getSettableOptions in core/modules/filter/src/Plugin/DataType/FilterFormat.php
Returns an array of settable values with labels for display.

... See full list

6 string references to 'filter_formats'
drupal6.php in core/modules/forum/tests/fixtures/drupal6.php
A database agnostic dump for testing purposes.
drupal6.php in core/modules/statistics/tests/fixtures/drupal6.php
A database agnostic dump for testing purposes.
drupal6.php in core/modules/book/tests/fixtures/drupal6.php
A database agnostic dump for testing purposes.
drupal6.php in core/modules/migrate_drupal/tests/fixtures/drupal6.php
A database agnostic dump for testing purposes.
FilterAPITest::testDependencyRemoval in core/modules/filter/tests/src/Kernel/FilterAPITest.php
Tests that filter format dependency removal works.

... See full list

File

core/modules/filter/filter.module, line 30

Code

function filter_formats(?AccountInterface $account = NULL) {
  $formats =& drupal_static(__FUNCTION__, []);
  // All available formats are cached for performance.
  if (!isset($formats['all'])) {
    $language_interface = \Drupal::languageManager()->getCurrentLanguage();
    if ($cache = \Drupal::cache()->get("filter_formats:{$language_interface->getId()}")) {
      $formats['all'] = $cache->data;
    }
    else {
      $formats['all'] = \Drupal::entityTypeManager()->getStorage('filter_format')
        ->loadByProperties([
        'status' => TRUE,
      ]);
      uasort($formats['all'], 'Drupal\\Core\\Config\\Entity\\ConfigEntityBase::sort');
      \Drupal::cache()->set("filter_formats:{$language_interface->getId()}", $formats['all'], Cache::PERMANENT, \Drupal::entityTypeManager()->getDefinition('filter_format')
        ->getListCacheTags());
    }
  }
  // If no user was specified, return all formats.
  if (!isset($account)) {
    return $formats['all'];
  }
  // Build a list of user-specific formats.
  $account_id = $account->id();
  if (!isset($formats['user'][$account_id])) {
    $formats['user'][$account_id] = [];
    foreach ($formats['all'] as $format) {
      if ($format->access('use', $account)) {
        $formats['user'][$account_id][$format->id()] = $format;
      }
    }
  }
  return $formats['user'][$account_id];
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.