1

I am new to drupal, I am using drupal 8, I have develop a custom form by extending FormBase controller and i am trying to render it using a custom controller like below.

$form = $this->formBuilder->getForm('Drupal\xyz\Forms\XyzActionsForm',$parameter);

return [
  '#form'=>$form
]

This works fine form me. But now i also want custom html below this form so try below code

$form = $this->formBuilder->getForm('Drupal\xyz\Forms\XyzActionsForm',$parameter);

$form = render(form);

$form .= '<div>custom html </div>';

return [
  '#markup'=>$form
]

But this didn't work for me, Can anyone let me know how to achieve this.

4

You have to use the power of render arrays.

For your example, I would use something like that :

$form_class = 'Drupal\xyz\Forms\XyzActionsForm';
$form = [
  'form' => $this->formBuilder->getForm($form_class, $parameter),
  'custom' => [
    '#type' => 'container',
    'inside' => [
      '#markup' => 'custom html', // Or use the power of render with html_tag type
    ],
  ],
];

return $form;

Then you can manage cache and other stuff on your render array and build your response with it.

  • That code work like a charm. Thanks for your quick reply on this – Udit Rawat Jul 23 '17 at 11:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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