Skip to main content
added 22 characters in body
Source Link

I finally found a way to get it to work after digging through the code for the EntityReferenceAutocompleteWidget. To enable automatic creation, I needed to edit the handler settings. Here's my code:

$fields['client'] = BaseFieldDefinition::create('entity_reference')
  ->setLabel(t('Client'))
  ->setSettings(array(
    'target_type'      => 'client',
    'handler'          => 'default',
    'handler_settings' => array(            // Added
      'auto_create'    => TRUE              // Added
    )
  ))
  ->setDisplayOptions('form', array(
    'type'     => 'entity_reference_autocomplete',
    'settings' => array(
      'match_operator' => 'CONTAINS',
      'size'           => 60,
      'placeholder'    => ''
    ),
    'weight'   => 0
  ))
  ->setRequired(TRUE);

I finally found a way to get it to work after digging through the code for the EntityReferenceAutocompleteWidget. To enable automatic creation, I needed to edit the handler settings. Here's my code:

$fields['client'] = BaseFieldDefinition::create('entity_reference')
  ->setLabel(t('Client'))
  ->setSettings(array(
    'target_type'      => 'client',
    'handler'          => 'default',
    'handler_settings' => array(            // Added
      'auto_create'    => TRUE
    )
  ))
  ->setDisplayOptions('form', array(
    'type'     => 'entity_reference_autocomplete',
    'settings' => array(
      'match_operator' => 'CONTAINS',
      'size'           => 60,
      'placeholder'    => ''
    ),
    'weight'   => 0
  ))
  ->setRequired(TRUE);

I finally found a way to get it to work after digging through the code for the EntityReferenceAutocompleteWidget. To enable automatic creation, I needed to edit the handler settings. Here's my code:

$fields['client'] = BaseFieldDefinition::create('entity_reference')
  ->setLabel(t('Client'))
  ->setSettings(array(
    'target_type'      => 'client',
    'handler'          => 'default',
    'handler_settings' => array(            // Added
      'auto_create'    => TRUE              // Added
    )
  ))
  ->setDisplayOptions('form', array(
    'type'     => 'entity_reference_autocomplete',
    'settings' => array(
      'match_operator' => 'CONTAINS',
      'size'           => 60,
      'placeholder'    => ''
    ),
    'weight'   => 0
  ))
  ->setRequired(TRUE);
deleted 776 characters in body
Source Link

I finally found a way to get it to work after digging through the code for the EntityReferenceAutocompleteWidget. To enable automatic creation, I needed to edit the handler settings. Here's my code:

$fields['client'] = BaseFieldDefinition::create('entity_reference')
  ->setLabel(t('Client'))
  ->setSettings(array(
    'target_type'      => 'client',
    'handler'          => 'default',
    'handler_settings' => array(            // Added
      'auto_create'    => TRUE,             // Added
      'target_bundles' => array('client')   // Added
    )
  ))
  ->setDisplayOptions('form', array(
    'type'     => 'entity_reference_autocomplete',
    'settings' => array(
      'match_operator' => 'CONTAINS',
      'size'           => 60,
      'placeholder'    => ''
    ),
    'weight'   => 0
  ))
  ->setRequired(TRUE);

There's just one problem with this: for some reason, the autocomplete part of the widget doesn't work anymore and I don't see why. It just loads forever. If I try to submit in that state, validation fails. To fix that, I had to redefine the buildForm method of the add form for my entity to add this workaround:

public function buildForm(array $form, FormStateInterface $form_state) {
  $form = parent::buildForm($form, $form_state);
  $form['client']['widget'][0]['target_id']['#selection_settings'] = NULL;

  return $form
}

This seems kind of ridiculous though; I don't see why it fixes anything. If someone knows why this is happening, please let me know.

I finally found a way to get it to work after digging through the code for the EntityReferenceAutocompleteWidget. To enable automatic creation, I needed to edit the handler settings. Here's my code:

$fields['client'] = BaseFieldDefinition::create('entity_reference')
  ->setLabel(t('Client'))
  ->setSettings(array(
    'target_type'      => 'client',
    'handler'          => 'default',
    'handler_settings' => array(            // Added
      'auto_create'    => TRUE,             // Added
      'target_bundles' => array('client')   // Added
    )
  ))
  ->setDisplayOptions('form', array(
    'type'     => 'entity_reference_autocomplete',
    'settings' => array(
      'match_operator' => 'CONTAINS',
      'size'           => 60,
      'placeholder'    => ''
    ),
    'weight'   => 0
  ))
  ->setRequired(TRUE);

There's just one problem with this: for some reason, the autocomplete part of the widget doesn't work anymore and I don't see why. It just loads forever. If I try to submit in that state, validation fails. To fix that, I had to redefine the buildForm method of the add form for my entity to add this workaround:

public function buildForm(array $form, FormStateInterface $form_state) {
  $form = parent::buildForm($form, $form_state);
  $form['client']['widget'][0]['target_id']['#selection_settings'] = NULL;

  return $form
}

This seems kind of ridiculous though; I don't see why it fixes anything. If someone knows why this is happening, please let me know.

I finally found a way to get it to work after digging through the code for the EntityReferenceAutocompleteWidget. To enable automatic creation, I needed to edit the handler settings. Here's my code:

$fields['client'] = BaseFieldDefinition::create('entity_reference')
  ->setLabel(t('Client'))
  ->setSettings(array(
    'target_type'      => 'client',
    'handler'          => 'default',
    'handler_settings' => array(            // Added
      'auto_create'    => TRUE
    )
  ))
  ->setDisplayOptions('form', array(
    'type'     => 'entity_reference_autocomplete',
    'settings' => array(
      'match_operator' => 'CONTAINS',
      'size'           => 60,
      'placeholder'    => ''
    ),
    'weight'   => 0
  ))
  ->setRequired(TRUE);
Source Link

I finally found a way to get it to work after digging through the code for the EntityReferenceAutocompleteWidget. To enable automatic creation, I needed to edit the handler settings. Here's my code:

$fields['client'] = BaseFieldDefinition::create('entity_reference')
  ->setLabel(t('Client'))
  ->setSettings(array(
    'target_type'      => 'client',
    'handler'          => 'default',
    'handler_settings' => array(            // Added
      'auto_create'    => TRUE,             // Added
      'target_bundles' => array('client')   // Added
    )
  ))
  ->setDisplayOptions('form', array(
    'type'     => 'entity_reference_autocomplete',
    'settings' => array(
      'match_operator' => 'CONTAINS',
      'size'           => 60,
      'placeholder'    => ''
    ),
    'weight'   => 0
  ))
  ->setRequired(TRUE);

There's just one problem with this: for some reason, the autocomplete part of the widget doesn't work anymore and I don't see why. It just loads forever. If I try to submit in that state, validation fails. To fix that, I had to redefine the buildForm method of the add form for my entity to add this workaround:

public function buildForm(array $form, FormStateInterface $form_state) {
  $form = parent::buildForm($form, $form_state);
  $form['client']['widget'][0]['target_id']['#selection_settings'] = NULL;

  return $form
}

This seems kind of ridiculous though; I don't see why it fixes anything. If someone knows why this is happening, please let me know.