8 – How to use DefaultSelection::getReferenceableEntities in custom controller?


My Goal:

I want to create a customized entity_autocomplete form field to better fit my needs compared to the default one – making some modifications to the HTML and JS that’s being used, as well as how submissions are handled.

But, just like in the default entity autocomplete we can type in 2 letters, and it finds all entities that contain these 2 letters.

Entity Reference Example

My Problem:

Unfortunately I have trouble figuring out how to use the existing “search feature” if you will call it that.

Some Considerations:

I looked into using search API, but this was similarly overwhelming. Considering that this functionality I’m looking for is already built into Drupal, I figure I can just re-implement it, without any additional modules, right? Really, I’m just re-designing the UI. And even if I need to beef it up in the future with a faster, resource saving search engine, I can just replace that aspect of my module/controller. (If someone can point out how to use Search API to do what I need, I’d be happy to listen)

What I’ve done so far:

I have already created my own custom route that points to a JSON controller which will accept the search parameters.

Based on all my research I figured I need to add use DrupalCoreEntityPluginEntityReferenceSelectionDefaultSelection; to the top.

I see that DefaultSelection::getReferenceableEntities seems to do exactly what I need it to do.

However, the constructor for this class has 9 different parameters, many of them classes which I know nothing about.

But there is a create method, which apparently allows you to inject configuration data, but also it requires a ContainerInterface.

So here’s where I’m lost…

How exactly do I make use of DefaultSelection::getReferenceableEntities in my custom controller?