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

  • There's no entityreference element type - maybe you meant entity_autocomplete? – Clive Feb 1 '18 at 11:25
  • not entity_autocomplete, i edited my question it's entity_reference of type paragraph, and the paragraph id is social_media – Mohammed ATIFI Feb 1 '18 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 '18 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 – Mohammed ATIFI Feb 1 '18 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 '18 at 14:31
0

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);

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.