I am creating a Drupal 8 form using custom content type. One of the fields to be created is a select list. The data that will be on the select list is stored in a MySQL database which needs to be queried out.

I have been asked to use hook_form_alter to create this list. I have been looking up a lot about this but I couldn't find a comprehensive guide on it.

  1. How do I know my form_id?
  2. How do I create this list?
  3. Where do I create and store this module?

Any suggestion will be greatly appreciated. Also, if you know a resource(book/website) that talks about it, please do mention it. It will be of great help.

  1. You can most easily get the form id by loading up the page with the form and inspecting the HTML. You will just need to change the hyphens (-) to underscores (_).
  2. How to generate the list will depend a lot on the nature of the information you're getting out of the database. If it's node based data out of the regular Drupal database, an EntityQuery is your best bet. If you're pulling data out of a custom table, it's up to you. Either way you'll create an array of key/value pairs. The "key" of the array items will become the select options' values, and the "value" will be the options' display.
  3. There are more resources for how to create a module out there than you can shake a stick at: https://lmgtfy.com/?q=how+to+create+a+drupal+8+module
| improve this answer | |

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