You are here

protected function FileTransferAuthorizeForm::setConnectionSettingsDefaults in Drupal 8

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/FileTransfer/Form/FileTransferAuthorizeForm.php \Drupal\Core\FileTransfer\Form\FileTransferAuthorizeForm::setConnectionSettingsDefaults()
  2. 10 core/lib/Drupal/Core/FileTransfer/Form/FileTransferAuthorizeForm.php \Drupal\Core\FileTransfer\Form\FileTransferAuthorizeForm::setConnectionSettingsDefaults()

Sets the default settings on a file transfer connection form recursively.

The default settings for the file transfer connection forms are saved in the database. The settings are stored as a nested array in the case of a settings form that has details or otherwise uses a nested structure. Therefore, to properly add defaults, we need to walk through all the children form elements and process those defaults recursively.

Parameters

$element: Reference to the Form API form element we're operating on.

$key: The key for our current form element, if any.

array $defaults: The default settings for the file transfer backend we're operating on.

1 call to FileTransferAuthorizeForm::setConnectionSettingsDefaults()
FileTransferAuthorizeForm::addConnectionSettings in core/lib/Drupal/Core/FileTransfer/Form/FileTransferAuthorizeForm.php
Generates the Form API array for a given connection backend's settings.

File

core/lib/Drupal/Core/FileTransfer/Form/FileTransferAuthorizeForm.php, line 292

Class

FileTransferAuthorizeForm
Provides the file transfer authorization form.

Namespace

Drupal\Core\FileTransfer\Form

Code

protected function setConnectionSettingsDefaults(&$element, $key, array $defaults) {

  // If we're operating on a form element which isn't a details, and we have
  // a default setting saved, stash it in #default_value.
  if (!empty($key) && isset($defaults[$key]) && isset($element['#type']) && $element['#type'] != 'details') {
    $element['#default_value'] = $defaults[$key];
  }

  // Now, we walk through all the child elements, and recursively invoke
  // ourselves on each one. Since the $defaults settings array can be nested
  // (because of #tree, any values inside details will be nested), if
  // there's a subarray of settings for the form key we're currently
  // processing, pass in that subarray to the recursive call. Otherwise, just
  // pass on the whole $defaults array.
  foreach (Element::children($element) as $child_key) {
    $this
      ->setConnectionSettingsDefaults($element[$child_key], $child_key, isset($defaults[$key]) && is_array($defaults[$key]) ? $defaults[$key] : $defaults);
  }
}