function form_example_form($form, &$form_state) {
      $form_state ['store_value'] =1;
      $form['submit_button'] = array(
        '#type' => 'submit',
        '#value' => t('Click Here!'),

  return $form;

How to do this in drupal 8

2 Answers 2


Insert the the value in $form_state:

$form_state->set('store_value', 1);

And then later for example in submitForm():

$value = $form_state->get('store_value');

Don't use getValue() or setValue(), because this is for submitted values.

If you are migrating more forms from D7 to D8, use this as reference:

$form_state is now a classed object

  • Correct, be aware of how form caching changed however, it might no longer behave the way you expect it to: drupal.stackexchange.com/questions/192402/…
    – Berdir
    Commented May 10, 2016 at 18:37
  • Good advice. It depends on what kind of data you store and what you want to do with it. Some things better work with a session, either to store all the data or in combination with form data.
    – 4uk4
    Commented May 10, 2016 at 20:59

$form_state is a instance of FormState object, you need use setValue($key, $value) method to store values on it.

Check the docs for additional info.

From docs:


string|array $key: Values are stored as a multi-dimensional associative array. If $key is a string, it will use $values[$key] = $value. If $key is an array, each element of the array will be used as a nested key. If $key = array('foo', 'bar') it will use $values['foo']['bar'] = $value.

mixed $value: The value to set.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.