Why doesn't loading a form in the view header work? I've been checking for the past 2 days on how to do a drupal_get_form() for Drupal 8 and everyone seems to give me the following answers which don't seem to work. Let's just say I want to take the following Basic Page creation form and embed it into a Views header:



The ID of the form is: node-page-form or node_page_form if we were using drupal_get_form() from Drupal 7. But this time its the following:

$form = \Drupal::formBuilder()->getForm(Drupal\user\Form\NodePageForm::class);

I'm assuming "NodePageForm" is the parameter I'm suppose to use. If not how can one find this out from viewing the source of that page?

I then put the following PHP code in the Views header of my page like this:



I click save and update... my header is blank. What did I do wrong? 

I'm trying to be as descriptive as I can explaining this problem. End result I just want to be able to take ANY form from any D8 page and be able to render it anywhere that takes PHP. That's my end goal. So no 3rd party modules as a solution.
sEntity forms should be built using the entity.form_builder service. You'll need an entity to work with, for a node your code might change to something like this:

$node = \Drupal::entityTypeManager()
  -&gt;getStorage('node')
  -&gt;create(['type' =&gt; 'page']);

$form = \Drupal::service('entity.form_builder')-&gt;getForm($node);

print \Drupal::service('renderer')-&gt;render($form);


As noted in the comments, this won't actually work in the context you're using it, regardless of how you render the form. The views_php area handler renders the output of the custom PHP using #markup, which will strip out the &lt;input&gt; and other form tags. You'll need a custom or contributed module, or to alter the views_php module directly, to make this work.

Here's a simple area handler that adds support for common form tags:

&lt;?php

namespace Drupal\foo\Plugin\views\area;

use Drupal\Component\Utility\Xss;
use Drupal\views_php\Plugin\views\area\ViewsPhp;

/**
 * Views area PHP text handler with support for common form tags.
 *
 * @ingroup views_area_handlers
 *
 * @ViewsArea("foo")
 */
class ViewsPhpForm extends ViewsPhp {

  public function render($empty = FALSE) {
    if (!$empty || !empty($this-&gt;options['empty'])) {
      $content = $this-&gt;renderViewsPhp($this-&gt;options['content']);
      $form_tags = ['form', 'input', 'select', 'fieldset', 'option', 'button', 'textarea', 'optgroup'];
      $allowed_tags = array_merge(Xss::getAdminTagList(), $form_tags);

      return [
        '#type' =&gt; 'inline_template',
        '#template' =&gt; Xss::filter($content, $allowed_tags),
      ];
    }

    return [];
  }

}


This code should be in src/Plugin/views/area/ViewsPhpForm.php and will need updating for the name of your module.

You'll also need to make views aware of it with a hook either in your .module file, or foo.views.inc:

/**
 * Implements hook_views_data().
 */
function foo_views_data() {
  $data['views']['php_with_form'] = [
    'title' =&gt; t('PHP with form'),
    'help' =&gt; t('Use PHP code with support for form tags.'),
    'area' =&gt; [
      'id' =&gt; 'foo',
    ],
  ];

  return $data;
}


After a cache rebuild you'll be able to add a new area handler, PHP with form, and your form should be rendered.