Developer Guide

Last updated on
22 February 2017

Drupal 7 will no longer be supported after January 5, 2025. Learn more and find resources for Drupal 7 sites

Adding a Key Select element to a form

Key adds a Form API element called key_select that behaves like a select element, but that is populated with available keys as options. This allows module developers to easily add a key field to a settings form, like this:

$form['secret_key'] = [
  '#type' => 'key_select',
  '#title' => t('Secret key'),
  ];

There are a couple of additional properties that can be used:

  • #key_filters An array of filters to apply to the list of keys. Filtering can be performed on any combination of key provider, key type, and key type group. Examples:
    • #key_filters = ['type' => 'mailchimp'] This would only display MailChimp keys.
    • #key_filters = ['provider' => 'file'] This would only display keys that use the File key provider.
    • #key_filters = ['type' => 'mailchimp', 'provider' => 'file'] This would only display MailChimp keys that use the File key provider.
    • #key_filters = ['type_group' => 'encryption'] This would only display encryption keys.
  • #key_description This is a boolean value that determines if information about keys is added to the element's description. It is TRUE by default and it prepends the description with the following text (with a link to the add key form):

    Choose an available key. If the desired key is not listed, create a new key.

    This can be disabled by setting #key_description to FALSE.

Using a Key in Drupal 7

Modules can retrieve information about keys or a specific key value.

Get all key configurations

key_get_keys()

Get a single key configuration

key_get_key($key_id)

Get a key value

key_get_key_value($key_id)

Help improve this page

Page status: No known problems

You can: