Drupal Commerce has a field widget, Commerce Entity Select that does some interesting things with the field and form API.
namespace Drupal\commerce\Plugin\Field\FieldWidget;
/**
* Plugin implementation of the 'commerce_entity_select' widget.
*
* @FieldWidget(
* id = "commerce_entity_select",
* label = @Translation("Entity select"),
* field_types = {
* "entity_reference"
* },
* multiple_values = TRUE
* )
*/
class EntitySelectWidget extends WidgetBase {}
Here's the widget settings form:
Here are the options you can change for the widget:
- If there is only one entity to select, hide the widget and select by default.
- Autocomplete threshold - Widget shows checkboxes if there are fewer than this many entities.
- Autocomplete size - field width.
- Autocomplete placeholder - dummy text.