You are here

function filter_default_format in Drupal 8

Same name and namespace in other branches
  1. 7 modules/filter/filter.module \filter_default_format()
  2. 9 core/modules/filter/filter.module \filter_default_format()

Returns the ID of the default text format for a particular user.

The default text format is the first available format that the user is allowed to access, when the formats are ordered by weight. It should generally be used as a default choice when presenting the user with a list of possible text formats (for example, in a node creation form).

Conversely, when existing content that does not have an assigned text format needs to be filtered for display, the default text format is the wrong choice, because it is not guaranteed to be consistent from user to user, and some trusted users may have an unsafe text format set by default, which should not be used on text of unknown origin. Instead, the fallback format returned by filter_fallback_format() should be used, since that is intended to be a safe, consistent format that is always available to all users.

Parameters

\Drupal\Core\Session\AccountInterface|null $account: (optional) The user account to check. Defaults to the currently logged-in user. Defaults to NULL.

Return value

string The ID of the user's default text format.

See also

filter_fallback_format()

25 calls to filter_default_format()
AreaTextTest::testAreaText in core/modules/views/tests/src/Kernel/Handler/AreaTextTest.php
ContextualLinksTest::setUp in core/modules/node/tests/src/FunctionalJavascript/ContextualLinksTest.php
DisplayFeedTest::testFeedFieldOutput in core/modules/views/tests/src/Functional/Plugin/DisplayFeedTest.php
Tests the rendered output for fields display.
DisplayFeedTest::testFeedOutput in core/modules/views/tests/src/Functional/Plugin/DisplayFeedTest.php
Tests the rendered output.
DisplayFeedTranslationTest::testFeedFieldOutput in core/modules/views/tests/src/Functional/Plugin/DisplayFeedTranslationTest.php
Tests the rendered output for fields display with multiple translations.

... See full list

1 string reference to 'filter_default_format'
Node::initializeIterator in core/modules/node/src/Plugin/migrate/source/d6/Node.php
Initializes the iterator with the source data.

File

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

Code

function filter_default_format(AccountInterface $account = NULL) {
  if (!isset($account)) {
    $account = \Drupal::currentUser();
  }

  // Get a list of formats for this user, ordered by weight. The first one
  // available is the user's default format.
  $formats = filter_formats($account);
  $format = reset($formats);
  return $format
    ->id();
}