You are here

public function AutosaveFormBuilder::prepareForm in Autosave Form 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/Form/AutosaveFormBuilder.php, line 223

Class

AutosaveFormBuilder
Provides form building and processing with AutosaveForm enabled.

Namespace

Drupal\autosave_form\Form

Code

public function prepareForm($form_id, &$form, FormStateInterface &$form_state) {
  $prevent_hooks = FALSE;
  if ($this
    ->isAutosaveTriggered($form_state)) {

    // There is no need of generating a new form build id after triggering
    // autosave.
    $form['#build_id'] = $form_state
      ->getUserInput()['form_build_id'];
    if ($form_state
      ->isCached()) {
      $prevent_hooks = TRUE;
    }
  }
  if ($prevent_hooks) {

    // Prevent running hooks.
    $module_handler = $this->moduleHandler;
    $theme_manager = $this->themeManager;
    $this->moduleHandler = new ModuleHandlerEmptyAlter();
    $this->themeManager = new ThemeManagerEmptyAlter();
  }
  parent::prepareForm($form_id, $form, $form_state);
  if ($prevent_hooks) {
    $this->moduleHandler = $module_handler;
    $this->themeManager = $theme_manager;
  }
}