Is it possible to create a form element of type markup and add a state to it, to make it visible on a condition? I can't seem to make it work and get the error:

Warning: Undefined array key "#type" in Drupal\Core\Form\FormHelper::processStates() (line 211 of core/lib/Drupal/Core/Form/FormHelper.php)

I need to just add some text in the form and make it visible on a condition.

Any help would be appreciated, thanks!

  • Hello @jacksparrow, could you add your code to the question please. My current suspicion is that the form element doesn't include '#type' => 'markup'. Typically the #type isn't needed for the 'markup' element but adding the States API into the mix may be making that required.
    – Gold
    Commented Mar 16, 2021 at 18:29
  • 1
    Thanks @Gold, yes actually the type markup was not added, but it is no longer valid, so #type item did the work! Just got it working, Thanks for your your help.
    – el sparrow
    Commented Mar 16, 2021 at 18:37

1 Answer 1


Adding form type item did the trick:

$form['my_incomes'] = [
      '#type' => 'item',
      '#markup' => $this->t('My other incomes'),
      '#states' => [
        'visible' => [
          [':input[name="marital_status"]' => [['value' => 'single'],['value'=>'couple']]],

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.