1

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')
      ->setLabel(t('Type'))
      ->setDescription(t('The Paragraphs type.'))
      ->setSetting('target_type', 'paragraph')
      ->setSetting('handler', 'default')
      ->setSetting('handler_settings',['target_bundles'=>
                      ['social_media'=>'social_media']
          ]);

    $form['social_media'] = $field;

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

the page is shown but without a form:

enter image description here

6
  • There's no entityreference element type - maybe you meant entity_autocomplete?
    – Clive
    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 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
    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 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
    Feb 1, 2018 at 14:31

2 Answers 2

1

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')
  ->setLabel(t('Skill'))
  ->setDescription(t('The ID of the skill'))
  ->setRevisionable(TRUE)
  ->setSetting('handler', 'default:paragraph')
  ->setSetting('handler_settings', ['target_bundles' => ['social_media' => 'social_media']])
  ->setSetting('handler_settings', ['negate' => 0])
  ->setTranslatable(TRUE)
  ->setDisplayOptions('form', [
    'type' => 'entity_reference_autocomplete',
    'weight' => 5,
    'settings' => [
      'match_operator' => 'CONTAINS',
      'size' => '60',
      'autocomplete_type' => 'tags',
      'placeholder' => '',
    ],
  ])
  ->setDisplayConfigurable('form', TRUE);
0

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.'))
  ->setCardinality(-1)
  ->setRevisionable(TRUE)
  ->setSetting('target_type', 'paragraph')
  ->setSetting('handler', 'default:paragraph')
  ->setSetting('handler_settings', ['target_bundles' => ['my_paragraph' => 'my_paragraph']])
  ->setTranslatable(TRUE)
  ->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);
2
  • No ->save(); in the end, is this right?
    – leymannx
    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. Jul 22, 2021 at 14:20

Your Answer

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

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