0

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?

  • 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 Nov 30 '18 at 8:47
  • I tried it that way - this is how I found these ideas. But I think I missed something – Tobias Krause Dec 4 '18 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 Dec 4 '18 at 19:24
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' => [],
  ])
  ...
0

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

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.