protected function alterRoutes(RouteCollection $collection) {
    if($route = $collection->get('entity.node.edit_form')) {
      $route->setDefault('_entity_form', '\Drupal\alerts\Form\EditAdvisoryForm');

Causes a permission error. The logs say

Path: /node/1277957/edit?destination=/admin/content. Drupal\Core\Http\Exception\CacheableAccessDeniedHttpException: in Drupal\Core\Routing\AccessAwareRouter->checkAccess() (line 117 of /app/web/core/lib/Drupal/Core/Routing/AccessAwareRouter.php).

It doesn't seem to prefill the form with the editable info. This is also not content type specific.

Please advise. Other posts close to this do not have an answer.

  • You need to define the custom edit form class as form handler in the entity type. Then you can use the form handler in _entity_form. See drupal.stackexchange.com/questions/283113/…. You can also replace the existing form handler in the entity type if you want to change the node edit form for the entire site without altering routes. – 4k4 Jan 5 at 7:22
  • The link you provided doesnt even look related to my question. There is no route file as this is customizing a system edit form. – David Latty Jan 5 at 8:06
  • This doesn't matter. I add an answer to clarify the issue in your code. – 4k4 Jan 5 at 8:29

_entity_form doesn't accept a form class directly, only via a form handler. See https://www.drupal.org/docs/drupal-apis/routing-system/structure-of-routes

_entity_form: It is similar to _form, but it will provide an edit form for an entity. Entity form handlers are defined in the entity metadata (annotation). For example, _entity_form: node.default will display the default node form. "node" in node.default refers to the entity ID, and "default" refers to the form handler key.

  • Thanks. Maybe you can give an example with details. When I attempt the hook with key string as my content type, I get error "PHP Fatal error: Uncaught Error: Call to a member function setFormClass() on null" i.e. that key isnt recognized – David Latty Jan 5 at 8:56
  • You have to use as key the entity type node. You can check the content type then in the form class. – 4k4 Jan 5 at 9:22
  • Apparently not giving answers is close worthy – David Latty Jan 5 at 19:28
  • I don't get bonus points for answering your comment about the linked hook_entity_type_build() you first said was not related? – 4k4 Jan 5 at 19:52
  • I stand corrected. You definitely do, though the same error received – David Latty Jan 5 at 20:12

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