FormTestStoragePageCacheForm.php 1.98 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\form_test\Form\FormTestStoragePageCacheForm.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
 */

namespace Drupal\form_test\Form;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;

class FormTestStoragePageCacheForm extends FormBase {

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'form_test_storage_page_cache';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form['title'] = array(
      '#type' => 'textfield',
      '#title' => 'Title',
      '#required' => TRUE,
    );

    $form['test_build_id_old'] = array(
      '#type' => 'item',
      '#title' => 'Old build id',
      '#markup' => 'No old build id',
    );

    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => 'Save',
    );

    $form['rebuild'] = array(
      '#type' => 'submit',
      '#value' => 'Rebuild',
      '#submit' => array(array($this, 'form_test_storage_page_cache_rebuild')),
    );

    $form['#after_build'] = array(array($this, 'form_test_storage_page_cache_old_build_id'));

    return $form;
  }

  /**
   * Form element #after_build callback: output the old form build-id.
   */
  function form_test_storage_page_cache_old_build_id($form) {
    if (isset($form['#build_id_old'])) {
59
      $form['test_build_id_old']['#plain_text'] = $form['#build_id_old'];
60 61 62 63 64 65 66 67 68 69 70
    }
    return $form;
  }

  /**
   * Form submit callback: Rebuild the form and continue.
   */
  function form_test_storage_page_cache_rebuild($form, FormStateInterface $form_state) {
    $form_state->setRebuild();
  }

71 72 73 74 75 76 77 78 79 80 81
  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    // Test using form cache when re-displaying a form due to validation
    // errors.
    if ($form_state->hasAnyErrors()) {
      $form_state->setCached();
    }
  }

82 83 84 85 86 87 88 89
  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    // Nothing must happen.
  }

}