function FileTransferAuthorizeForm::buildForm

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

Overrides FormInterface::buildForm

File

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

Class

FileTransferAuthorizeForm
Provides the file transfer authorization form.

Namespace

Drupal\Core\FileTransfer\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
    // Get all the available ways to transfer files.
    $available_backends = $this->getRequest()
        ->getSession()
        ->get('authorize_filetransfer_info', []);
    if (empty($available_backends)) {
        $this->messenger()
            ->addError($this->t('Unable to continue, no available methods of file transfer'));
        return [];
    }
    if (!$this->getRequest()
        ->isSecure()) {
        $form['information']['https_warning'] = [
            '#prefix' => '<div class="messages messages--error">',
            '#markup' => $this->t('WARNING: You are not using an encrypted connection, so your password will be sent in plain text. <a href=":https-link">Learn more</a>.', [
                ':https-link' => 'https://www.drupal.org/https-information',
            ]),
            '#suffix' => '</div>',
        ];
    }
    // Decide on a default backend.
    $authorize_filetransfer_default = $form_state->getValue([
        'connection_settings',
        'authorize_filetransfer_default',
    ]);
    if (!$authorize_filetransfer_default) {
        $authorize_filetransfer_default = key($available_backends);
    }
    $form['information']['main_header'] = [
        '#prefix' => '<h3>',
        '#markup' => $this->t('To continue, provide your server connection details'),
        '#suffix' => '</h3>',
    ];
    $form['connection_settings']['#tree'] = TRUE;
    $form['connection_settings']['authorize_filetransfer_default'] = [
        '#type' => 'select',
        '#title' => $this->t('Connection method'),
        '#default_value' => $authorize_filetransfer_default,
        '#weight' => -10,
    ];
    
    /*
     * Here we create two submit buttons. For a JS enabled client, they will
     * only ever see submit_process. However, if a client doesn't have JS
     * enabled, they will see submit_connection on the first form (when picking
     * what filetransfer type to use, and submit_process on the second one (which
     * leads to the actual operation).
     */
    $form['submit_connection'] = [
        '#prefix' => "<br style='clear:both'/>",
        '#name' => 'enter_connection_settings',
        '#type' => 'submit',
        '#value' => $this->t('Enter connection settings'),
        '#weight' => 100,
    ];
    $form['submit_process'] = [
        '#name' => 'process_updates',
        '#type' => 'submit',
        '#value' => $this->t('Continue'),
        '#weight' => 100,
    ];
    // Build a container for each connection type.
    foreach ($available_backends as $name => $backend) {
        $form['connection_settings']['authorize_filetransfer_default']['#options'][$name] = $backend['title'];
        $form['connection_settings'][$name] = [
            '#type' => 'container',
            '#attributes' => [
                'class' => [
                    "filetransfer-{$name}",
                    'filetransfer',
                ],
            ],
            '#states' => [
                'visible' => [
                    'select[name="connection_settings[authorize_filetransfer_default]"]' => [
                        'value' => $name,
                    ],
                ],
            ],
        ];
        // We can't use #prefix on the container itself since then the header won't
        // be hidden and shown when the containers are being manipulated via JS.
        $form['connection_settings'][$name]['header'] = [
            '#markup' => '<h4>' . $this->t('@backend connection settings', [
                '@backend' => $backend['title'],
            ]) . '</h4>',
        ];
        $form['connection_settings'][$name] += $this->addConnectionSettings($name);
        // Start non-JS code.
        if ($form_state->getValue([
            'connection_settings',
            'authorize_filetransfer_default',
        ]) == $name) {
            // Change the submit button to the submit_process one.
            $form['submit_process']['#attributes'] = [];
            unset($form['submit_connection']);
            // Activate the proper filetransfer settings form.
            $form['connection_settings'][$name]['#attributes']['style'] = 'display:block';
            // Disable the select box.
            $form['connection_settings']['authorize_filetransfer_default']['#disabled'] = TRUE;
            // Create a button for changing the type of connection.
            $form['connection_settings']['change_connection_type'] = [
                '#name' => 'change_connection_type',
                '#type' => 'submit',
                '#value' => $this->t('Change connection type'),
                '#weight' => -5,
                '#attributes' => [
                    'class' => [
                        'filetransfer-change-connection-type',
                    ],
                ],
            ];
        }
        // End non-JS code.
    }
    return $form;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.