Im writing a module. One thing it will do is alter the menu edit form.

I've been looking at core code and examples on the internet and can't really figure out when to extend Drupals core classes, use a hook or an event listener. I get event listeners can access a lot more stuff that the hooks provided by core.

Do Any of you pro drupalerz have preferences/feelings about this?

Any patterns you see?

Have you experienced friction by extending Drupals core Form or Controller classes (my preference) vs using hooks or an event listener?

  • To what event do you want to listen? Form alter is still a hook. In general I wouldn't replace form alter by extending the form class, too much risk this conflicts with other modules if they choose to replace the same class.
    – 4uk4
    Apr 25, 2019 at 12:53
  • More of a general question. I guess I could have asked: when to OO or when to use exposed hooks or the event system. Im more prone to use the event system so my code can be in classes. Just an old dude trying to figure out how to write good Drupal code. Looking at core I see that the use of hooks is prolific. I'll try to follow what patters I see in there.
    – johnfallen
    Apr 25, 2019 at 13:08


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.