You are here

protected function KeyFormBase::updateKeyInput in Key 8

Update the Key Input plugin.

Parameters

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

1 call to KeyFormBase::updateKeyInput()
KeyFormBase::buildForm in src/Form/KeyFormBase.php
Form constructor.

File

src/Form/KeyFormBase.php, line 411

Class

KeyFormBase
Base form for key add and edit forms.

Namespace

Drupal\key\Form

Code

protected function updateKeyInput(FormStateInterface $form_state) {

  /* @var $key \Drupal\key\Entity\Key */
  $key = $this->entity;

  /* @var $plugin \Drupal\key\Plugin\KeyPluginInterface */
  $plugin = $key
    ->getKeyInput();

  // Get the current key value data.
  $key_value_data = $form_state
    ->get('key_value');

  // Determine which Key Input plugin should be used.
  $key_input_id = 'none';
  if ($key
    ->getKeyProvider()
    ->getPluginDefinition()['key_value']['accepted']) {
    $key_input_id = $key
      ->getKeyType()
      ->getPluginDefinition()['key_value']['plugin'];
  }

  // Set the Key Input plugin.
  $key
    ->setPlugin('key_input', $key_input_id);

  // Set the plugin's configuration to the default. It may be
  // overridden below.
  $configuration = $plugin
    ->defaultConfiguration();

  // Clear the current key value. It may be overridden below.
  $key_value_data['current'] = '';
  $use_original_key_value = FALSE;

  // If an original key exists, one of the following conditions must
  // be met in order to use the key value from it:
  // - The key value was not obscured when the form first loaded
  // - The original key provider is the same as the current one
  //   AND the original key type is the same as the current one.
  if ($this->originalKey) {

    // If the key value is not obscured.
    if (empty($key_value_data['obscured'])) {
      $use_original_key_value = TRUE;
    }

    // If the original key provider is the same as the current one.
    if ($this->originalKey
      ->getKeyProvider()
      ->getPluginId() == $key
      ->getKeyProvider()
      ->getPluginId()) {

      // If the original key type is the same as the current one.
      if ($this->originalKey
        ->getKeyType()
        ->getPluginId() == $key
        ->getKeyType()
        ->getPluginId()) {
        $use_original_key_value = TRUE;
      }
    }
  }

  // If the original key value can be used.
  if ($use_original_key_value) {

    // Use the configuration from the original key's plugin.
    $configuration = $this->originalKey
      ->getKeyInput()
      ->getConfiguration();

    // Set the current key value.
    $key_value_data['current'] = !empty($key_value_data['obscured']) ? $key_value_data['obscured'] : $key_value_data['processed_original'];
  }
  $plugin
    ->setConfiguration($configuration);
  $form_state
    ->setValue('key_input_settings', []);
  $form_state
    ->getUserInput()['key_input_settings'] = [];
  $form_state
    ->set('key_value', $key_value_data);
}