Passing parameter to node add form through URL node/add/termid using routing.yml drupal 8

 

It is used to send extra parameter in arg without using query param, Actually have overwritten the default routing by creating new route and disable the existing route.

Passing term id to node/add  form by url node/add/{termid},  if you pass like you will get page not found error, because it will consider as unknown route, according to drupal node/add and node/add/1 is different, to make it work this , created new route in custom module and called/used the default core node controllers by routing  file.

Add this in modulename.routing.yml file

modulename.createnewroute:
 path: '/node/add/{node_type}/{termid}'
 defaults:
  _controller: '\Drupal\node\Controller\NodeController::add'
  _title_callback: '\Drupal\node\Controller\NodeController::addPageTitle'
 requirements:
  _node_add_access: 'node:{node_type}'
 options:
  _node_operation_route: TRUE
  parameters:
   node_type:
    with_config_overrides: TRUE
modulename.restrictexistingcontenttype:
 path: '/node/add/yourcontentypewhenaccessingwithouttermid'
 requirements:
  _access: 'FALSE'