2

I defined a custom entity and for a entity reference field I want to set the view reference widget and a view/ display to be used instead of an autocomplete.

I tried many different settings - nothing worked. The latest version not working is:

$fields['user'] = BaseFieldDefinition::create('entity_reference')
  ->setLabel(t('User account'))
  ->setDescription(t('The user account this profile is connected to'))
  ->setSetting('target_type','user')
  ->setDisplayOptions('view', array(
    'label' => 'inline',
    'type' => 'string',
    'weight' => -6,
  ))
  ->setDisplayOptions('form', array(
    'type' => 'views',
    'settings' => array(
      'handler_settings' => array(
        'view_name' => 'users_without_profile',
        'display_name' => 'entity_reference_1',
        'arguments' => []
      ),
      'weight' => 20
    )

  ))
  ->setDisplayConfigurable('form', TRUE)
  ->setDisplayConfigurable('view', TRUE);

For the form view setting I also tried

  ->setDisplayOptions('form', array(
    'type' => 'views',
    'settings' => array(
      'view_name' => 'users_without_profile',
      'display_name' => 'entity_reference_1',
      'arguments' => [],
      'weight' => 20
    )
  ))

and

  ->setDisplayOptions('form', array(
    'type' => 'entity_reference_autocomplete',
    'settings' => array(
        'handler' => 'views',
        'handler_settings' => array(
            'view_name' => 'users_without_profile',
            'display_name' => 'entity_reference_1',
            'arguments' => [],
        ),          
        'weight' => 20
    )
  ))

No success. Can somebody give me a hint?

3
  • 4
    One way to find out: Add a field through the UI, configure it exactly as you want to, then pop to admin -> Configuration -> Config Synchronisation -> Export -> Single Item. You can export the field instance and/or storage into the text area to find out exactly how the settings array should be structured
    – Clive
    Commented Nov 30, 2018 at 8:47
  • I tried it that way - this is how I found these ideas. But I think I missed something Commented Dec 4, 2018 at 18:56
  • If you look at the export for a reference field you'll see a top level settings item - this has a handler and handler_settings, which are the ones you want to manipulate. I've added an example
    – Clive
    Commented Dec 4, 2018 at 19:24

2 Answers 2

2

It seems that the previous answer was missing a small detail: the information about the view needs to be wrapped into an array with key 'view' llike following:

$fields['user'] = BaseFieldDefinition::create('entity_reference')
  ->setLabel(t('User account'))
  ->setDescription(t('The user account this profile is connected to'))
  ->setSetting('target_type','user')
  ->setSetting('handler', 'views')
  ->setSetting('handler_settings', [
    'view' => [
      'view_name' => 'users_without_profile',
      'display_name' => 'entity_reference_1',
      'arguments' => [],
    ]
  ])

In this way it is finally working

0

The handler settings are part of the base field itself, rather than being a setting for the form display.

This should work:

$fields['user'] = BaseFieldDefinition::create('entity_reference')
  ->setLabel(t('User account'))
  ->setDescription(t('The user account this profile is connected to'))
  ->setSetting('target_type','user')
  ->setSetting('handler', 'views')
  ->setSetting('handler_settings', [
    'view_name' => 'users_without_profile',
    'display_name' => 'entity_reference_1',
    'arguments' => [],
  ])
  ...

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.