function filter_formats

Same name in other branches
  1. 7.x modules/filter/filter.module \filter_formats()
  2. 8.9.x core/modules/filter/filter.module \filter_formats()
  3. 10 core/modules/filter/filter.module \filter_formats()
  4. 11.x 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()

18 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.
Editor::getAttachments in core/modules/quickedit/src/Plugin/InPlaceEditor/Editor.php
Returns the attachments for this editor.
Editor::getAttachments in core/modules/editor/src/Plugin/InPlaceEditor/Editor.php
Returns the attachments for this editor.
FilterCrudTest::testTextFormatCrud in core/modules/filter/tests/src/Kernel/FilterCrudTest.php
Tests CRUD operations for text formats and filters.

... See full list

4 string references to 'filter_formats'
drupal6.php in core/modules/migrate_drupal/tests/fixtures/drupal6.php
A database agnostic dump for testing purposes.
drupal6.php in core/modules/aggregator/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.
filter_formats_reset in core/modules/filter/filter.module
Resets the text format caches.

File

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

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.