You are here

public static function KeySelect::processKeySelect in Key 8

Processes a key select list form element.

Parameters

array $element: The form element to process.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

array $complete_form: The complete form structure.

Return value

array The processed element.

File

src/Element/KeySelect.php, line 55

Class

KeySelect
Provides a select form element that displays available keys.

Namespace

Drupal\key\Element

Code

public static function processKeySelect(array &$element, FormStateInterface $form_state, array &$complete_form) {

  // Get the list of available keys and define the options.
  $options = \Drupal::service('key.repository')
    ->getKeyNamesAsOptions($element['#key_filters']);
  $element['#options'] = $options;

  // Prefix the default description with information about keys,
  // unless disabled.
  if ($element['#key_description']) {
    $original_description = isset($element['#description']) ? $element['#description'] : '';

    // @todo this causes escaping.
    $key_description = t('Choose an available key. If the desired key is not listed, <a href=":link">create a new key</a>.', [
      ':link' => Url::fromRoute('entity.key.add_form')
        ->toString(),
    ]);
    $element['#description'] = $key_description . ' ' . $original_description;
  }
  return $element;
}