2

If I form alter a form, I see the form_id as MY_FORM_ID; I see this in the $form_id parameter to hook_form_alter as well as in the $form_state->build_info['form_id']. In the rendered output of the form, however, I see the id attribute has something like MY-FORM-ID--2, with a --INT modifier. If I output the same form twice on a page, while logged in the modifier is changed with each render (e.g. MY-FORM-ID--2 and MY-FORM-ID--4), however the modifier is not changed when viewing as anon. This creates an instance where two elements have the same id attribute which is no good! :(

  1. Where does core add the modifier?
  2. How can I add a modifier?
4
  • Huh, I think we need to see the snippet how exactly you are outputting the form twice on a page...
    – leymannx
    Commented Nov 12, 2018 at 20:30
  • I have a webform referenced on a node. The node is being rendered twice on the page.
    – sonfd
    Commented Nov 12, 2018 at 20:31
  • As far as I know this modifier behavior is from core as I also see the same behavior with other, non webform, forms.
    – sonfd
    Commented Nov 12, 2018 at 20:37
  • 3
    @sonfd, then you have to add a unique identifier to the form id when you instantiate the form class, in this case the most obvious would be the node id. See drupal.stackexchange.com/questions/223269/…
    – 4uk4
    Commented Nov 12, 2018 at 20:38

0

Browse other questions tagged or ask your own question.