1

I recently found that unlike Drupal 7, D8 has a different form id for addition and edit of nodes. Is there a specific reason for that? Because this makes the hook_form_FORM_ID_alter() less granular as we now have to alter like mymodule_form_node_form_alter() and put the specific node type in there.

Sorry, could not find a discussion on this. Must be something very basic that I missed.

Thanks

6

I'd rather say, added granularity is exactly what has been achieved by introducing entity form ID's specific to their entity type, bundle and operation.

You can now target the form for a specific entity type and bundle with a specific operation when using hook_form_FORM_ID_alter(). Before this you had to check the bundle yourself and also figure out whether the node already had an ID or not.

The form ID will be composed of entity type identifier, bundle, operation and the _form suffix. (Compare EntityForm::getFormID().)

If you require a more generic form alter for ALL node forms, you can use hook_form_BASE_FORM_ID_alter() which will be invoked with the base form ID that is ENTITYTYPE_form for entities, so node_form for content entities.

The invocation order along with a description of all three hooks is actually well documented in the description of hook_form_alter().

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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