PermissionHandler
class PermissionHandler implements PermissionHandlerInterface (View source)
Provides the available permissions based on yml files.
To define permissions you can use a $module.permissions.yml file. This file defines machine names, human-readable names, restrict access (if required for security warning), and optionally descriptions for each permission type. The machine names are the canonical way to refer to permissions for access checking.
If your module needs to define dynamic permissions you can use the permission_callbacks key to declare a callable that will return an array of permissions, keyed by machine name. Each item in the array can contain the same keys as an entry in $module.permissions.yml.
Here is an example from the core filter module (comments have been added):
Traits
Wrapper methods for \Drupal\Core\StringTranslation\TranslationInterface.
Properties
| protected TranslationInterface | $stringTranslation | The string translation service. |
from StringTranslationTrait |
| protected ModuleHandlerInterface | $moduleHandler | The module handler. |
|
| protected YamlDiscovery | $yamlDiscovery | The YAML discovery class to find all .permissions.yml files. |
|
| protected ControllerResolverInterface | $controllerResolver | The controller resolver. |
Methods
Translates a string to the current language or to a given language.
Formats a string containing a count of items.
Returns the number of plurals supported by a given language.
Gets the string translation service.
Constructs a new PermissionHandler.
Gets the YAML discovery.
Gets all available permissions.
Determines whether a module provides some permissions.
Builds all permissions provided by .permissions.yml files.
Sorts the given permissions by provider name and title.
Returns all module names.
Details
protected TranslatableMarkup
t(string $string, array $args = [], array $options = [])
Translates a string to the current language or to a given language.
See \Drupal\Core\StringTranslation\TranslatableMarkup::__construct() for important security information and usage guidelines.
In order for strings to be localized, make them available in one of the ways supported by the
protected
formatPlural($count, $singular, $plural, array $args = [], array $options = [])
Formats a string containing a count of items.
protected
getNumberOfPlurals($langcode = NULL)
Returns the number of plurals supported by a given language.
protected TranslationInterface
getStringTranslation()
Gets the string translation service.
$this
setStringTranslation(TranslationInterface $translation)
Sets the string translation service to use.
__construct(ModuleHandlerInterface $module_handler, TranslationInterface $string_translation, ControllerResolverInterface $controller_resolver)
Constructs a new PermissionHandler.
protected YamlDiscovery
getYamlDiscovery()
Gets the YAML discovery.
array
getPermissions()
Gets all available permissions.
bool
moduleProvidesPermissions(string $module_name)
Determines whether a module provides some permissions.
protected array[]
buildPermissionsYaml()
Builds all permissions provided by .permissions.yml files.
protected array[]
sortPermissions(array $all_permissions = [])
Sorts the given permissions by provider name and title.
protected string[]
getModuleNames()
Returns all module names.