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()
->getStorage('node')
->create(['type' => 'page']);
$form = \Drupal::service('entity.form_builder')->getForm($node);
print \Drupal::service('renderer')->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 <input>
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:
<?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->options['empty'])) {
$content = $this->renderViewsPhp($this->options['content']);
$form_tags = ['form', 'input', 'select', 'fieldset', 'option', 'button', 'textarea', 'optgroup'];
$allowed_tags = array_merge(Xss::getAdminTagList(), $form_tags);
return [
'#type' => 'inline_template',
'#template' => 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' => t('PHP with form'),
'help' => t('Use PHP code with support for form tags.'),
'area' => [
'id' => '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.
<?php ?>
tag?