0

In a table, I keep a collection of nodes, text and forms that should be rendered on particular generated pages. I run through a loop that builds an output variable based on the the tables type and values. Worked fine in D7 and the text and node pieces are working fine in D8, after some adjustments. The piece I'm having trouble with is adding a form to the output variable.

In D7:

$output = "a variety of html";
$formname = "triagezipform"; //would be filled in by field value
$form = drupal_get_form($formname);
$output .= render($form);

In D8:

$output = "a variety of html";
$formname = "triagezipform"; //would be filled in by field value
$form = \Drupal::formBuilder()->getForm('Drupal\triage\Form\'.$formname);
$output .= render($form);

I see the form properly at the end of the function that builds the output, but as it goes through Drupal page rendering the Form elements are removed.

Pre-page rendering:

<div class='triage-action-form ta-contrast-background noprint '>
  <form class="triagezipform" data-drupal-selector="triagezipform"               action="form_action_p_pvdeGsVG5zNF_XLGPTvYSKCf43t8qZYSwcfZl2uzM" method="post" id="triagezipform"
                  accept-charset="UTF-8">
<div class="form-item js-form-item form-type-textfield js-form-type-textfield form-item-triage-zip js-form-item-triage-zip form-group">
  <label for="edit-triage-zip" class="control-label">Enter zipcode where your legal issue is</label>
  <input data-drupal-selector="edit-triage-zip" class="form-text form-control" type="text"id="edit-triage-zip" name="triage_zip" value="04643" size="20" maxlength="5"/>
</div>
</form>

Post-page rendering:

<div class="form-item js-form-item form-type-textfield js-form-type-textfield form-item-triage-zip js-form-item-triage-zip form-group">
          Enter zipcode where your legal issue is
</div>

Any ideas of how I get forms to show properly when being built this way?

0

How are you then using $output? Are you putting it into ['#markup'] in your build array?

If so markup content will strip all sorts out. So either you can put $form as a separate item in your build array and there's no need to run it through render()

Or you can use drupal's "inline_template" rather than #markup in the build array

|improve this answer|||||

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.