filter_formats

filter_formats(AccountInterface $account = NULL)

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()

File

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

Code

function filter_formats(AccountInterface $account = NULL) {
  $formats = &drupal_static(__FUNCTION__, array());

  // 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::entityManager()->getStorage('filter_format')->loadByProperties(array('status' => TRUE));
      uasort($formats['all'], 'Drupal\Core\Config\Entity\ConfigEntityBase::sort');
      \Drupal::cache()->set("filter_formats:{$language_interface->getId()}", $formats['all'], Cache::PERMANENT, \Drupal::entityManager()->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] = array();
    foreach ($formats['all'] as $format) {
      if ($format->access('use', $account)) {
        $formats['user'][$account_id][$format->id()] = $format;
      }
    }
  }

  return $formats['user'][$account_id];
}
doc_Drupal
2016-10-29 09:14:54
Comments
Leave a Comment

Please login to continue.