Skip to main content
Shortened link
Source Link
leymannx
  • 18.8k
  • 6
  • 72
  • 126

Core's link field actually uses a '#type' => 'url' form element as base and if the field is configured to support internal links it switches to '#type' => 'entity_autocomplete'. See https://git.drupalcode.org/project/drupal/blob/8.8.0/core/modules/link/src/Plugin/Field/FieldWidget/LinkWidget.php#L186php L186-203

So if you just want to be able to select entities go for '#type' => 'entity_autocomplete'.

$form['cta'] = [
  '#type' => 'entity_autocomplete',
  '#target_type' => 'node',
];

And if you want to be able to type external URLs there as well you need to override the element validation yourself. Nonetheless you need to add your own simple '#type' => 'textfield' element to pick up a user entered title. Same as the link widget does it.

Core's link field actually uses a '#type' => 'url' form element as base and if the field is configured to support internal links it switches to '#type' => 'entity_autocomplete'. See https://git.drupalcode.org/project/drupal/blob/8.8.0/core/modules/link/src/Plugin/Field/FieldWidget/LinkWidget.php#L186-203

So if you just want to be able to select entities go for '#type' => 'entity_autocomplete'.

$form['cta'] = [
  '#type' => 'entity_autocomplete',
  '#target_type' => 'node',
];

And if you want to be able to type external URLs there as well you need to override the element validation yourself. Nonetheless you need to add your own simple '#type' => 'textfield' element to pick up a user entered title. Same as the link widget does it.

Core's link field actually uses a '#type' => 'url' form element as base and if the field is configured to support internal links it switches to '#type' => 'entity_autocomplete'. See /core/modules/link/src/Plugin/Field/FieldWidget/LinkWidget.php L186-203

So if you just want to be able to select entities go for '#type' => 'entity_autocomplete'.

$form['cta'] = [
  '#type' => 'entity_autocomplete',
  '#target_type' => 'node',
];

And if you want to be able to type external URLs there as well you need to override the element validation yourself. Nonetheless you need to add your own simple '#type' => 'textfield' element to pick up a user entered title. Same as the link widget does it.

Source Link
leymannx
  • 18.8k
  • 6
  • 72
  • 126

Core's link field actually uses a '#type' => 'url' form element as base and if the field is configured to support internal links it switches to '#type' => 'entity_autocomplete'. See https://git.drupalcode.org/project/drupal/blob/8.8.0/core/modules/link/src/Plugin/Field/FieldWidget/LinkWidget.php#L186-203

So if you just want to be able to select entities go for '#type' => 'entity_autocomplete'.

$form['cta'] = [
  '#type' => 'entity_autocomplete',
  '#target_type' => 'node',
];

And if you want to be able to type external URLs there as well you need to override the element validation yourself. Nonetheless you need to add your own simple '#type' => 'textfield' element to pick up a user entered title. Same as the link widget does it.