1

I have two modules that both extend NodeForm the usual way:

function MY_MODULE_entity_type_alter(array &$entity_info) {
  $entity_info['node']->setFormClass('default', 'Drupal\MY_MODULE\Form\MyModuleNodeForm');
  $entity_info['node']->setFormClass('edit', 'Drupal\MY_MODULE\Form\MyModuleNodeForm');
}

As far as I can tell (both from actually looking into what setFormClass() does and simply trying out) this is a race condition that only one of them can win. Although they both want to deal with different content types and what they do would not conflict at all, they can't coexist. With the old, 7-style alter and submit functions, this could be solved but what would be the proper 8-ish way to do it? Both modules are mine, I can combine them if necessary but that wouldn't be really elegant.

1
  • A module dealing with a specific content type shouldn't replace the form class. In D8 content types are configuration, so you should first consider to configure this too, for example a form mode, which is content type specific by default. Put there for example a custom widget, which in this case doesn't need to replace a core widget, it can have a custom plugin id auto discovered by Drupal, so you don't need a hook at all and have a 100 % OOP code not interfering with other code. If you want to use D7 style hooks use those which allow you to write content type specific code.
    – 4uk4
    May 27, 2018 at 7:32

1 Answer 1

3

You have two options. Either go back to using _form_alter() hooks to handle it, or you can extend twice:

module 1 node for extends module 2 node form which extends node form. Then make sure to call the parent::validateForm and parent::submitForm from your validate and submit handlers.

2
  • These will be in-house modules so any solution will do. But two modules out there in the wild can clash this way. Not nice. Thanks, anyway.
    – Gábor
    May 26, 2018 at 23:25
  • If you are putting out modules in the wild, it should be done with _form_alter() hooks for exactly that reason.
    – Jaypan
    May 26, 2018 at 23:33

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.