You are here

public function FormBuilder::prepareForm in Secure Pages 8

Prepares a structured form array.

Adds required elements, executes any hook_form_alter functions, and optionally inserts a validation token to prevent tampering.

Parameters

string $form_id: A unique string identifying the form for validation, submission, theming, and hook_form_alter functions.

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form. Passed in here so that hook_form_alter() calls can use it, as well.

Overrides FormBuilder::prepareForm

File

src/FormBuilder.php, line 22
Contains \Drupal\securepages\FormBuilder.

Class

FormBuilder
FormBuilder decorator for the core formbuilder.

Namespace

Drupal\securepages

Code

public function prepareForm($form_id, &$form, FormStateInterface &$form_state) {

  // Override \Drupal\Core\Form\FormBuilder::renderPlaceholderFormAction(),
  // the default form action lazy builder, with Secure Pages' variant. Since
  // Secure Pages allows specific forms to opt in, we need to not only
  // override the #lazy_builder callback but also the arguments: the form ID
  // also needs to be known.
  // Only update the action if it is not already set.
  $config = \Drupal::config('securepages.settings');
  if ($config
    ->get('enabled') && !isset($form['#action'])) {

    // Instead of setting an actual action URL, we set the placeholder, which
    // will be replaced at the very last moment. This ensures forms with
    // dynamically generated action URLs don't have poor cacheability.
    // Use the proper API to generate the placeholder, when we have one. See
    // https://www.drupal.org/node/2562341.
    $placeholder = 'form_action_' . hash('crc32b', __METHOD__);

    //      $form['#attached']['placeholders'][$placeholder] = [
    //        '#lazy_builder' => ['securepages.form_builder:renderPlaceholderFormAction', [$form_id]],
    //      ];
    $form['#action'] = $placeholder;
  }
  return parent::prepareForm($form_id, $form, $form_state);
}