Created
April 5, 2018 22:03
-
-
Save swichers/1cf7f70120e40794efb10ba1003dce7d to your computer and use it in GitHub Desktop.
Adding the entity bundle to the autocomplete results
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/docroot/modules/custom/custom_utils/custom_utils.services.yml b/docroot/modules/custom/custom_utils/custom_utils.services.yml | |
index 006b5d51..98868011 100644 | |
--- a/docroot/modules/custom/custom_utils/custom_utils.services.yml | |
+++ b/docroot/modules/custom/custom_utils/custom_utils.services.yml | |
@@ -1,3 +1,11 @@ | |
services: | |
custom_utils.controller.fieldscontroller: | |
- class: Drupal\custom_utils\Controller\FieldsController | |
\ No newline at end of file | |
+ class: Drupal\custom_utils\Controller\FieldsController | |
+ | |
+ custom_utils.route_subscriber: | |
+ class: Drupal\custom_utils\Routing\AutocompleteRouteSubscriber | |
+ tags: | |
+ - { name: event_subscriber } | |
+ custom_utils.autocomplete_matcher: | |
+ class: Drupal\custom_utils\EntityAutocompleteMatcher | |
+ arguments: ['@plugin.manager.entity_reference_selection'] | |
\ No newline at end of file | |
diff --git a/docroot/modules/custom/custom_utils/src/Controller/EntityAutocompleteController.php b/docroot/modules/custom/custom_utils/src/Controller/EntityAutocompleteController.php | |
new file mode 100644 | |
index 00000000..336eba20 | |
--- /dev/null | |
+++ b/docroot/modules/custom/custom_utils/src/Controller/EntityAutocompleteController.php | |
@@ -0,0 +1,34 @@ | |
+<?php | |
+ | |
+namespace Drupal\custom_utils\Controller; | |
+ | |
+use Drupal\Core\KeyValueStore\KeyValueStoreInterface; | |
+use Drupal\custom_utils\EntityAutocompleteMatcher; | |
+use Symfony\Component\DependencyInjection\ContainerInterface; | |
+ | |
+class EntityAutocompleteController extends \Drupal\system\Controller\EntityAutocompleteController { | |
+ | |
+ /** | |
+ * The autocomplete matcher for entity references. | |
+ */ | |
+ protected $matcher; | |
+ | |
+ /** | |
+ * {@inheritdoc} | |
+ */ | |
+ public function __construct(EntityAutocompleteMatcher $matcher, KeyValueStoreInterface $key_value) { | |
+ $this->matcher = $matcher; | |
+ $this->keyValue = $key_value; | |
+ } | |
+ | |
+ /** | |
+ * {@inheritdoc} | |
+ */ | |
+ public static function create(ContainerInterface $container) { | |
+ return new static( | |
+ $container->get('custom_utils.autocomplete_matcher'), | |
+ $container->get('keyvalue')->get('entity_autocomplete') | |
+ ); | |
+ } | |
+ | |
+} | |
\ No newline at end of file | |
diff --git a/docroot/modules/custom/custom_utils/src/EntityAutocompleteMatcher.php b/docroot/modules/custom/custom_utils/src/EntityAutocompleteMatcher.php | |
new file mode 100644 | |
index 00000000..a39071a7 | |
--- /dev/null | |
+++ b/docroot/modules/custom/custom_utils/src/EntityAutocompleteMatcher.php | |
@@ -0,0 +1,46 @@ | |
+<?php | |
+ | |
+namespace Drupal\custom_utils; | |
+ | |
+use Drupal\Component\Utility\Html; | |
+use Drupal\Component\Utility\Tags; | |
+ | |
+class EntityAutocompleteMatcher extends \Drupal\Core\Entity\EntityAutocompleteMatcher { | |
+ | |
+ /** | |
+ * {@inheritdoc} | |
+ * | |
+ * Custom getMatches() implementation to include bundle in output. | |
+ */ | |
+ public function getMatches($target_type, $selection_handler, $selection_settings, $string = '') { | |
+ $matches = []; | |
+ | |
+ $options = $selection_settings + [ | |
+ 'target_type' => $target_type, | |
+ 'handler' => $selection_handler, | |
+ ]; | |
+ $handler = $this->selectionManager->getInstance($options); | |
+ | |
+ if (isset($string)) { | |
+ // Get an array of matching entities. | |
+ $match_operator = !empty($selection_settings['match_operator']) ? $selection_settings['match_operator'] : 'CONTAINS'; | |
+ $entity_labels = $handler->getReferenceableEntities($string, $match_operator, 10); | |
+ | |
+ // Loop through the entities and convert them into autocomplete output. | |
+ foreach ($entity_labels as $bundle => $values) { | |
+ foreach ($values as $entity_id => $label) { | |
+ $key = "$bundle: $label ($entity_id)"; | |
+ // Strip things like starting/trailing white spaces, line breaks and | |
+ // tags. | |
+ $key = preg_replace('/\s\s+/', ' ', str_replace("\n", '', trim(Html::decodeEntities(strip_tags($key))))); | |
+ // Names containing commas or quotes must be wrapped in quotes. | |
+ $key = Tags::encode($key); | |
+ $matches[] = ['value' => $key, 'label' => "$bundle: $label"]; | |
+ } | |
+ } | |
+ } | |
+ | |
+ return $matches; | |
+ } | |
+ | |
+} | |
\ No newline at end of file | |
diff --git a/docroot/modules/custom/custom_utils/src/Routing/AutocompleteRouteSubscriber.php b/docroot/modules/custom/custom_utils/src/Routing/AutocompleteRouteSubscriber.php | |
new file mode 100644 | |
index 00000000..ec41f6d6 | |
--- /dev/null | |
+++ b/docroot/modules/custom/custom_utils/src/Routing/AutocompleteRouteSubscriber.php | |
@@ -0,0 +1,16 @@ | |
+<?php | |
+ | |
+namespace Drupal\custom_utils\Routing; | |
+ | |
+use Drupal\Core\Routing\RouteSubscriberBase; | |
+use Symfony\Component\Routing\RouteCollection; | |
+ | |
+class AutocompleteRouteSubscriber extends RouteSubscriberBase { | |
+ | |
+ public function alterRoutes(RouteCollection $collection) { | |
+ if ($route = $collection->get('system.entity_autocomplete')) { | |
+ $route->setDefault('_controller', '\Drupal\custom_utils\Controller\EntityAutocompleteController::handleAutocomplete'); | |
+ } | |
+ } | |
+ | |
+} | |
\ No newline at end of file |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment