function filter_get_roles_by_format

Same name in other branches
  1. 7.x modules/filter/filter.module \filter_get_roles_by_format()
  2. 9 core/modules/filter/filter.module \filter_get_roles_by_format()
  3. 8.9.x core/modules/filter/filter.module \filter_get_roles_by_format()
  4. 11.x core/modules/filter/filter.module \filter_get_roles_by_format()

Retrieves a list of roles that are allowed to use a given text format.

Parameters

\Drupal\filter\FilterFormatInterface $format: An object representing the text format.

Return value

array An array of role names, keyed by role ID.

7 calls to filter_get_roles_by_format()
FilterDefaultConfigTest::testInstallation in core/modules/filter/tests/src/Kernel/FilterDefaultConfigTest.php
Tests installation of default formats.
FilterDefaultConfigTest::testUpdateRoles in core/modules/filter/tests/src/Kernel/FilterDefaultConfigTest.php
Tests that changes to FilterFormat::$roles do not have an effect.
FilterFormatAccessTest::testFormatRoles in core/modules/filter/tests/src/Functional/FilterFormatAccessTest.php
Tests if text format is available to a role.
FilterFormatEditForm::form in core/modules/filter/src/FilterFormatEditForm.php
FilterFormatFormBase::form in core/modules/filter/src/FilterFormatFormBase.php

... See full list

File

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

Code

function filter_get_roles_by_format(FilterFormatInterface $format) {
    // Handle the fallback format upfront (all roles have access to this format).
    if ($format->isFallbackFormat()) {
        return array_map(fn(RoleInterface $role) => $role->label(), Role::loadMultiple());
    }
    // Do not list any roles if the permission does not exist.
    $permission = $format->getPermissionName();
    if (empty($permission)) {
        return [];
    }
    $roles = array_filter(Role::loadMultiple(), fn(RoleInterface $role) => $role->hasPermission($permission));
    return array_map(fn(RoleInterface $role) => $role->label(), $roles);
}

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