0

Similar to how you can reference a user or a node, I would like to reference a custom entity:

      $fields['department_id'] = BaseFieldDefinition::create('entity_reference')
      ->setLabel(t('Department'))
      ->setDescription(t('Select the department for this business.'))
      ->setSettings(array(
        'target_type' => 'department_register'
      ))
      ->setSetting('handler', 'default')
      ->setDisplayOptions('view', [
        'label' => 'inline',
        'type' => 'department',
        'weight' => -10,
      ])
      ->setDisplayOptions('form', [
        'type' => 'entity_reference_autocomplete',
        'settings' => [
          'match_operator' => 'CONTAINS',
          'match_limit' => 10,
          'size' => 60,
          'placeholder' => ''
        ],
        'weight' => -10,
      ])
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayConfigurable('view', TRUE);

Where 'target_type' => 'department_register' is my custom department entity

I'm stuck in how to connect my department entity. How can I setup the right plugin configuration for my autocomplete form field which should be able to select the name of the department?

3
  • You haven't asked a question, or clearly identified a problem. Please review your post, and try to narrow down one or both of those two things.
    – Jaypan
    Aug 9, 2022 at 2:46
  • Where 'target_type' => 'business_register' is my custom business entity - Shouldn't that be your department entity?
    – 4uk4
    Aug 9, 2022 at 7:19
  • I've updated my code with the right entity name. The question is at the bottom. Thanks Aug 9, 2022 at 10:59

1 Answer 1

2

Not using bundles, entity_reference_autocomplete needs to know which field to use for it's autocomplete functionality, so did you add the:

"label" = "name",

to your business_register(target entity) entity_keys={...} ? (in the annotations) Where 'name' is the target column. (label = title when using bundles)

1
  • 1
    Great! works, thank you so much Aug 19, 2022 at 16:09

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.