Drupal 8. Clicking the upload button on Form1 should open UploadForm in a modal dialog.

 * Callback to display a modal.
public function ajaxSubmit(&$form, FormStateInterface $form_state) {
  $op = $form_state->getTriggeringElement()['#return_value'];
  switch ($op) {
    case 'upload':
      $modal = new OpenModalDialogCommand(
        t('Upload file(s)'), 
            $this->nid, $this->foo, $this->bar))));

    // ... other cases.

  $response = new AjaxResponse();
  return $response;

I'm not able to pass the variables to UploadForm. $this-> properties don't appear in UploadForm.

\Drupal::formBuilder()->getForm('UploadForm', $this->nid, $this->foo, $this->bar));

The form also fails if I replace the variables by local variables

$nid, $foo, $bar

But it works when I replace them by

'28', 'foo', 'bar'

The non-ajax version of the form uses a redirect to a form route (my_module.uploadform) and works OK. Is there a way to pass the parameters to UploadForm?

  • Does your class define and assign values to $this->nid, $this->foo, and $this->bar? If it doesn't, the code doesn't work. The same is true for $nid, $foo, and $bar since the method you are showing doesn't have any of those local variables. If it works when you use constant values, then the problem is merely using not defined properties/variables.
    – apaderno
    Apr 28, 2016 at 23:42

1 Answer 1


Solved. Thanks @kiamlaluno. I was convinced the variables were OK. But after your comment and a fresh look I saw what I was missing: $this->nid, $this->foo, $this->bar are optional parameters for the UploadForm formbuilder. I was passing only nid and bar, but missing foo, which messed-up things. So, instead of:

\Drupal::formBuilder()->getForm('UploadForm', $this->nid, $this->bar) //don't need foo

I needed to call:

\Drupal::formBuilder()->getForm('UploadForm', $this->nid, null, $this->bar)

A bit obvious, I'm afraid...

  • 1
    Hey, if this is the right answer to you question, please mark it. So others will recognize easily that this problem got solved sufficiently. It's totally fine to accept your own answer.
    – leymannx
    Dec 21, 2016 at 13:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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