Skip to main content
Fixed undefined variable $entity; Fixed quote character typo; Applied coding standards
Source Link
leymannx
  • 18.8k
  • 6
  • 72
  • 126

If you have a link field attached to any entity like a node bundle, or user or ... then you can use a code like this  :

// Create an empty representative entity.
$node = \Drupal::service('entity_type.manager')
  ->getStorage('node')->create(array(
            'type'['type' => 'article'
        )
    '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')
                                    ->load('node.article.default'); 

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

If you have a link field attached to any entity like a node bundle, or user or ... then you can use a code like this  :

//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')
                                    ->load('node.article.default');
//Get the body field widget and add it to the form
if ($widget = $entity_form_display->getRenderer('body')) { //Returns the widget class
  $items = $entity->get('body''); //Returns the FieldItemsList interface
  $items->filterEmptyItems();
  $form['body'] = $widget->form($items, $form, $form_state); //Builds the widget form and attach it to your form
  $form['body']['#access'] = $items->access('edit');
}

If you have a link field attached to any entity like a node bundle, or user or ... then you can use a code like this:

// Create an empty representative entity.
$node = \Drupal::service('entity_type.manager')
  ->getStorage('node')->create(['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')->load('node.article.default'); 

// 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.
  $items->filterEmptyItems();
  $form['body'] = $widget->form($items, $form, $form_state); // Builds the widget form and attach it to your form.
  $form['body']['#access'] = $items->access('edit');
}
Source Link

If you have a link field attached to any entity like a node bundle, or user or ... then you can use a code like this :

//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')
                                    ->load('node.article.default');
//Get the body field widget and add it to the form
if ($widget = $entity_form_display->getRenderer('body')) { //Returns the widget class
  $items = $entity->get('body''); //Returns the FieldItemsList interface
  $items->filterEmptyItems();
  $form['body'] = $widget->form($items, $form, $form_state); //Builds the widget form and attach it to your form
  $form['body']['#access'] = $items->access('edit');
}