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)'),
render(\Drupal::formBuilder()->getForm('UploadForm',
$this->nid, $this->foo, $this->bar))));
break;
// ... other cases.
}
$response = new AjaxResponse();
$response->setAttachments($form['#attached']);
$response->addCommand($modal);
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?
$this->nid
,$this->foo
, and$this->bar
? If it doesn't, there aren't wonders 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. – kiamlaluno♦ Apr 28 '16 at 23:42