How can I prepopulate a field's value when creating a new entity
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;
}