Page callback: Autocompletes any plugin system tied to a plugin UI plugin.
The passed plugin_id indicates the specific plugin_ui plugin that is in use here. The documentation within the annotation of that plugin will contain a manager for the plugins that need to be autocompleted allowing this function to autocomplete plugins for any plugin type.
$plugin_id: The plugin id for the calling plugin.
object JsonResponse
function system_plugin_autocomplete($plugin_id) {
$string_typed = drupal_container()
->get('request')->query
->get('q');
$string_typed = drupal_explode_tags($string_typed);
$string = drupal_strtolower(array_pop($string_typed));
$matches = array();
if ($string) {
$plugin_ui = Drupal::service('plugin.manager.system.plugin_ui')
->getDefinition($plugin_id);
$manager = Drupal::service($plugin_ui['manager']);
$titles = array();
foreach ($manager
->getDefinitions() as $plugin_id => $plugin) {
$titles[$plugin_id] = $plugin[$plugin_ui['title_attribute']];
}
$matches = preg_grep("/\\b" . $string . "/i", $titles);
}
return new JsonResponse($matches);
}