Skip to main content
added note on value storage
Source Link
mvdgun
  • 1.9k
  • 13
  • 22

The value in the $form_state (actually everything) is lost after the PHP process ends. So there is no way to preserve your my_variable value via the $form_state. Your best option is to store this value in the session I guess.

Example:

  public function validateForm(array &$form, FormStateInterface $form_state) {
    parent::validateForm($form, $form_state);

    $session = \Drupal::request()->getSession();
    $session->set('my_variable', $session->get('my_variable') + 1);
  }

  public function buildForm(array $form, FormStateInterface $form_state) {
    $session = \Drupal::request()->getSession();

    if (empty($session->get('my_variable'))) {
      $session->set('my_variable', 1);
    }
  }

Do note that the data gets lost as soon as the user clears his cookies and/or the sessions gets invalidated. If you need a permanent global storage (for all users) use the State API. For a permanent storage per user use the Database API.

The value in the $form_state (actually everything) is lost after the PHP process ends. So there is no way to preserve your my_variable value via the $form_state. Your best option is to store this value in the session I guess.

Example:

  public function validateForm(array &$form, FormStateInterface $form_state) {
    parent::validateForm($form, $form_state);

    $session = \Drupal::request()->getSession();
    $session->set('my_variable', $session->get('my_variable') + 1);
  }

  public function buildForm(array $form, FormStateInterface $form_state) {
    $session = \Drupal::request()->getSession();

    if (empty($session->get('my_variable'))) {
      $session->set('my_variable', 1);
    }
  }

The value in the $form_state (actually everything) is lost after the PHP process ends. So there is no way to preserve your my_variable value via the $form_state. Your best option is to store this value in the session I guess.

Example:

  public function validateForm(array &$form, FormStateInterface $form_state) {
    parent::validateForm($form, $form_state);

    $session = \Drupal::request()->getSession();
    $session->set('my_variable', $session->get('my_variable') + 1);
  }

  public function buildForm(array $form, FormStateInterface $form_state) {
    $session = \Drupal::request()->getSession();

    if (empty($session->get('my_variable'))) {
      $session->set('my_variable', 1);
    }
  }

Do note that the data gets lost as soon as the user clears his cookies and/or the sessions gets invalidated. If you need a permanent global storage (for all users) use the State API. For a permanent storage per user use the Database API.

Source Link
mvdgun
  • 1.9k
  • 13
  • 22

The value in the $form_state (actually everything) is lost after the PHP process ends. So there is no way to preserve your my_variable value via the $form_state. Your best option is to store this value in the session I guess.

Example:

  public function validateForm(array &$form, FormStateInterface $form_state) {
    parent::validateForm($form, $form_state);

    $session = \Drupal::request()->getSession();
    $session->set('my_variable', $session->get('my_variable') + 1);
  }

  public function buildForm(array $form, FormStateInterface $form_state) {
    $session = \Drupal::request()->getSession();

    if (empty($session->get('my_variable'))) {
      $session->set('my_variable', 1);
    }
  }