I added a number of fields to a node content type in Drupal 8. I want to create a custom form and add the field widget form (the one used during node creation) to this form. How can I achieve this?


Answering my own question, I ended up using the following:

//Create an empty representative entity
$node = \Drupal::service('entity_type.manager')->getStorage('node')->create(array(
                'type' => 'article'

//Get the EntityFormDisplay (i.e. the default Form Display) of this content type
$entity_form_display = \Drupal::service('entity_type.manager')->getStorage('entity_form_display')

$form['#parents'] = []; // Mandatory, otherwise the widget class throws an error

//Get the body field widget and add it to the form
if ($widget = $entity_form_display->getRenderer('body')) { //Returns the widget class
  $items = $node->get('body'); //Returns the FieldItemsList interface
  $form['body'] = $widget->form($items, $form, $form_state); //Builds the widget form and attach it to your form
  $form['body']['#access'] = $items->access('edit');

see EntityFormDisplay::buildForm

| improve this answer | |
  • 1
    $form['#parents'] needs to be set to something or you'll get an exception - an empty array will do. – George Jan 18 '18 at 6:39
  • 2
    I can confirm that it does not work without setting $form['#parents'] = array(); beforehand. Maybe add this to your answer? – PhilippVerpoort Mar 30 '18 at 18:55
  • 1
    I'm using this for a field on a node where the user doesn't have permission to edit the node. It's specifically a paragraph field and it is not being fully shown in the custom form. How can I alter the permission so the user has full access to the field, or do I need to allow them to edit the node? – Web Assistant Feb 28 '19 at 16:28

Also you can use this code

$entity = \Drupal::entityManager()->getStorage('node')->create([
        'type' => 'article'
$form_state->set('node', $entity );       
$form_display = EntityFormDisplay::collectRenderDisplay($entity, 'default');
$form_display->buildForm($entity, $form, $form_state);

I found this solution here

| improve this answer | |
  • Drupal::entityManager() is deprecated and shouldn't be used. This comment is for people who want to make the code work for Drupal 8.9/ 9 – Hodba Khalaf Jun 29 at 17:03

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.