This module provides the ability for site administrators to manage sitewide keys, which can be used, for example, to integrate with external services. Keys are available for use by other modules using a Drupal service provided by Key.
To manage keys, visit admin/config/system/key
. When creating a file, enter a name for the key and select the key provider to be used. Additional settings for the selected key provider may be required. For instance, if the File key provider is selected, enter a path to the file that contains the key.
Key leverages the Drupal 8 Plugin API for key providers, so that other modules can define additional key providers through the Key Plugin Manager. A key provider defines a method for retrieving a key, along with settings specific to that key provider, which are saved when creating a Key entity. Each plugin needs to register, as an annotation, the storage method used within the plugin.
For convention, it is recommended to use one the following storage method values:
- File - Stored in a file and retrieved via the local filesystem or by using a stream wrapper
- Configuration - Stored with Drupal’s built-in configuration management (settings, entities)
- Database - Stored as a field in a database record
- Remote - Retrieved via a remote service call
Modules can retrieve information about keys or a specific key value by making a call to the Key Manager service. It is best practice to
inject the service into your own service, form,
or controller. The following examples assume the use of the \Drupal
object for brevity, but the examples can be extrapolated to fit
the use case of your module.
Drupal::service('key_repository')->getKeys();
Drupal::service('key_repository')->getKey($key_id);
Drupal::service('key_repository')->getKey();
Drupal::service('key_repository')->getKey($key_id)->getKeyValue();