How can I prepopulate a field's value when creating a new entity I have a custom entity (foo) which contains an entity reference field to another custom entity (bar). I would like to create a URL on the bar's unique page to create a new foo entity, where the entity reference is pre-populated with the correct target_id.

For example, a link to /admin/structure/my_module/foo/add?target_id=2501

Is this feature already available? Can I add something to the entity buildForm method to prepopulate my value? 

class FooForm extends ContentEntityForm {
  public function buildForm(array $form, FormStateInterface $form_state) {
    //SOMETHING LIKE THIS?
    //if (isset(REQUEST::get('target_id'))) {
    //  $form_state['edit-bar-id-0-target-id'] = REQUEST::get('target_id');
    //}
    /* @var $entity \Drupal\my_module\Entity\Foo */
    $form = parent::buildForm($form, $form_state);
    $entity = $this-&gt;entity;
    return $form;
  }
}


I'm not familiar enough with the structure of the $form_state or best practices on get() to be comfortable testing this.
After adding dependency injection of a requestStack, this works.

/**
 * @var Symfony\Component\HttpFoundation\RequestStack
 */
protected $requestStack;

/**
 * Constructor.
 *
 * @param Symfony\Component\HttpFoundation\RequestStack $request_stack
 */
public function __construct(RequestStack $request_stack) {
  $this-&gt;requestStack = $request_stack;
}

/**
 * {@inheritdoc}
 */
public static function create(ContainerInterface $container) {
  return new static(
    $container-&gt;get('request_stack')
  );
}

public function buildForm(array $form, FormStateInterface $form_state)
  /* @var $entity \Drupal\my_module\Entity\Foo */
  $target_id = $this-&gt;requestStack-&gt;getCurrentRequest()-&gt;query-&gt;get('target_id');
  $this-&gt;entity-&gt;set('bar_id', $target_id);
  $form = parent::buildForm($form, $form_state);
  $entity = $this-&gt;entity;

  return $form;
}