Entity form additional arguments I have normal Drupal8 Content entity, and I'd like to set up some additional arguments for the entity form builder.

Ie I have entity form path like so: /tilaus/uusi/{tilaus_type} and I'd like to add additional parameter, like so: /tilaus/uusi/{tilaus_type}/{some_additional_argument}. Preferably in a way that would have both of those urls working and I could handle the missing argument in buildForm method.

I've been searching for a while now, but can't find anything about adding stuff to entity form paths. Normal forms are fine and I know how to handle those. It seems that I just can't figure out the entity routing.

So: How to add additional arguments to entity form url and builder?

Cheers,

Janne
An entity add form is built with a new entity. And when you create this entity you can use parameters for this. See this example from the block module.

The new route with the additional parameter:

block.routing.yml

block.admin_add:
  path: '/admin/structure/block/add/{plugin_id}/{theme}'
  defaults:
    _controller: '\Drupal\block\Controller\BlockAddController::blockAddConfigureForm'
    theme: null
    _title: 'Configure block'
  requirements:
    _permission: 'administer blocks'


This adds the parameter theme.

And the controller for this route:

BlockAddController.php

 public function blockAddConfigureForm($plugin_id, $theme) {
    // Create a block entity with prepopulated values.
    $entity = $this-&gt;entityManager()-&gt;getStorage('block')-&gt;create([
      'plugin' =&gt; $plugin_id,
      'theme' =&gt; $theme,
    ]);

    return $this-&gt;entityFormBuilder()-&gt;getForm($entity);
  }


Here the additional parameter theme is used when creating the entity.

Using both routes at the same time is no problem, the standard entity route configured in the entity annotations will still be in place.

Another way to use parameters in entity forms is in the third parameter $form_state_additions: 

$form_state_additions['langcode'] = $langcode;
$form = \Drupal::service('entity.form_builder')-&gt;getForm($entity, 'default', $form_state_additions);


This example is from the source of the entity formbuilder:

https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Entity%21EntityFormBuilderInterface.php/8.2.x