3

Is there a convention for the form IDs?

I see that core sometimes uses module_name.form_name and sometimes module_name_form_name.
In my case, I'm using module_name.form_name.

5
  • 1
    Would you need to put the dot/period in hook_form_FORM_ID_alter() ?
    – No Sssweat
    Commented Nov 12, 2019 at 20:59
  • 4
    Can you name atleast one core module where the dot/period gets used?
    – No Sssweat
    Commented Nov 12, 2019 at 21:06
  • The dot looks odd. Rhyme.
    – leymannx
    Commented Nov 12, 2019 at 21:10
  • The form id needs to be valid as part of a function name so that the hooks system can react correctly to it. Commented Nov 12, 2019 at 21:22
  • 2
    @NoSssweat It seems I was tired when I started searching yesterday and I see now I saw yesterday ban.ip_manager (a name of a service) and I confused this with the form id, in this case the form id is ban_ip_form :-D Commented Nov 12, 2019 at 21:45

1 Answer 1

6

Drupal core doesn't use the dot in the form IDs because, as the documentation for FormInterface::getFormId() says, the form ID should be a unique string that can be a valid PHP function name, since it's used in hook implementation names such as hook_form_FORM_ID_alter().

The form IDs Drupal core uses follow the <module name>_<form name> schema, so you will find form IDs like the following.

  • user_admin_settings
  • action_admin_manage
  • layout_builder_add_block

As long as the form ID is unique, any scheme would work. The convention to prefix an ID with the module name (i.e. the module machine name) has been used from Drupal since many years.

2
  • Thanks, it seems I had a problem reading the code yesterday :-D drupal.stackexchange.com/questions/288223/… Commented Nov 12, 2019 at 21:46
  • 1
    Sometimes it's not that quick to find a function/method whose documentation helps in understanding how Drupal works. :)) I think this question will help new Drupal developers, as most of them would use a generic ID, not an ID that can also be used as part of a function/method name.
    – avpaderno
    Commented Nov 12, 2019 at 22:07

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.