I need to create a config page, that is able to add social media information ( name and icon ), the user must be able to add as many social medias as it wants. To do so, I'we created a paragraph with two fields: name & icon, then I want to add this paragraph as an entity reference field programmaticaly in my config page, the paragraph machine name is social_media.

I tried this, but it didn't work:

public function buildForm(array $form, FormStateInterface $form_state) {

    $form['social_media_fieldset'] = [
      '#type' => 'fieldset',
      '#title' => $this->t('Social Media Configuration'),
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,

    $field = BaseFieldDefinition::create('entity_reference')
      ->setDescription(t('The Paragraphs type.'))
      ->setSetting('target_type', 'paragraph')
      ->setSetting('handler', 'default')

    $form['social_media'] = $field;

    return parent::buildForm($form, $form_state);

the page is shown but without a form:

enter image description here

  • There's no entityreference element type - maybe you meant entity_autocomplete?
    – Clive
    Commented Feb 1, 2018 at 11:25
  • not entity_autocomplete, i edited my question it's entity_reference of type paragraph, and the paragraph id is social_media Commented Feb 1, 2018 at 13:36
  • 1
    Are you talking about an entity reference field attached to an entity type? That’s a different thing, you can’t use it in a custom form, it only applies to an entity form
    – Clive
    Commented Feb 1, 2018 at 14:18
  • yes, that's exactly it, so it's not possible to create an entity reference field attached to a paragraph entity type in a custom form, i would appreciate if there is another solution to achieve this Commented Feb 1, 2018 at 14:22
  • 1
    That’s right, by definition a form widget for an entity field can only be used on a form built for the related entity. Seems counter intuitive until you get under the hood, it does make sense. I’d assume whatever you’re building will have to be manually coded
    – Clive
    Commented Feb 1, 2018 at 14:31

2 Answers 2


The following code did the trick for me when adding the field to custom entity. This provides a reference to the paragraph and the revision. Just to note out that when the paragraph is edited, the reference field will still refer to the old revision. The field type is entity_reference_revisions and my paragraph type

$fields['social_media_id'] = BaseFieldDefinition::create('entity_reference_revisions')
  ->setDescription(t('The ID of the skill'))
  ->setSetting('handler', 'default:paragraph')
  ->setSetting('handler_settings', ['target_bundles' => ['social_media' => 'social_media']])
  ->setSetting('handler_settings', ['negate' => 0])
  ->setDisplayOptions('form', [
    'type' => 'entity_reference_autocomplete',
    'weight' => 5,
    'settings' => [
      'match_operator' => 'CONTAINS',
      'size' => '60',
      'autocomplete_type' => 'tags',
      'placeholder' => '',
  ->setDisplayConfigurable('form', TRUE);

The code below uses the Paragraphs Classic widget for the form display and allows to add as many paragraph items as you need

$fields['my_paragraph'] = BaseFieldDefinition::create('entity_reference_revisions')
  ->setLabel(t('My paragraph'))
  ->setDescription(t('My paragraph description.'))
  ->setSetting('target_type', 'paragraph')
  ->setSetting('handler', 'default:paragraph')
  ->setSetting('handler_settings', ['target_bundles' => ['my_paragraph' => 'my_paragraph']])
  ->setDisplayOptions('form', [
    'type' => 'entity_reference_paragraphs',
    'weight' => 12,
    'settings' => [
      'title' => 'Paragraph',
      'title_plural' => 'Paragraphs',
      'edit_mode' => 'open',
      'add_mode' => 'dropdown',
      'form_display_mode' => 'default',
      'default_paragraph_type' => '',
  ->setDisplayConfigurable('form', TRUE)
  ->setDisplayConfigurable('view', TRUE);
  • No ->save(); in the end, is this right?
    – leymannx
    Commented Jul 20, 2021 at 23:40
  • I use the code to create fields for a custom entity (function baseFieldDefinitions) so I just need to return $fields. Commented Jul 22, 2021 at 14:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.