diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/Controller/TermAutocompleteController.php b/core/modules/taxonomy/lib/Drupal/taxonomy/Controller/TermAutocompleteController.php index cdd2a08..08a87c7 100644 --- a/core/modules/taxonomy/lib/Drupal/taxonomy/Controller/TermAutocompleteController.php +++ b/core/modules/taxonomy/lib/Drupal/taxonomy/Controller/TermAutocompleteController.php @@ -7,15 +7,17 @@ namespace Drupal\taxonomy\Controller; +use Drupal\Component\Utility\Tags; +use Drupal\Component\Utility\Unicode; +use Drupal\Component\Utility\String; use Drupal\Core\Controller\ControllerInterface; -use Drupal\Core\Database\Connection; +use Drupal\Core\Entity\Query\QueryInterface; +use Drupal\field\FieldInfo; +use Drupal\taxonomy\TermStorageControllerInterface; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\JsonResponse; -use Drupal\Component\Utility\Tags; -use Drupal\Component\Utility\Unicode; -use Drupal\Component\Utility\String; /** * Returns responses for Views UI routes. @@ -23,20 +25,40 @@ class TermAutocompleteController implements ControllerInterface { /** - * Database Service Object. + * Entity query factor. + * + * @var \Drupal\Core\Entity\Query\QueryInterface + */ + protected $entityQuery; + + /** + * Field info service. + * + * @var \Drupal\field\FieldInfo + */ + protected $fieldInfo; + + /** + * Term storage controller. * - * @var \Drupal\Core\Database\Connection + * @var \Drupal\taxonomy\TermStorageControllerInterface */ - protected $database; + protected $termStorageController; /** * Constructs a new \Drupal\taxonomy\Controller\TermAutocompleteController object. * - * @param \Drupal\Core\Database\Connection $database - * The database connection. + * @param \Drupal\Core\Entity\Query\QueryInterface $entityQuery + * The entity query service. + * @param \Drupal\field\FieldInfo $fieldInfo + * The field info service. + * @param \Drupal\taxonomy\TermStorageControllerInterface $termStorageController + * The term storage controller. */ - public function __construct(Connection $database) { - $this->database = $database; + public function __construct(QueryInterface $entityQuery, FieldInfo $fieldInfo, TermStorageControllerInterface $termStorageController) { + $this->entityQuery = $entityQuery; + $this->fieldInfo = $fieldInfo; + $this->termStorageController = $termStorageController; } /** @@ -44,7 +66,9 @@ public function __construct(Connection $database) { */ public static function create(ContainerInterface $container) { return new static( - $container->get('database') + $container->get('entity.query'), + $container->get('field.info'), + $container->get('plugin.manager.entity')->getStorageController('taxonomy_term') ); } @@ -84,7 +108,7 @@ public function autocomplete(Request $request, $field_name) { $tags_typed = $request->query->get('q'); // Make sure the field exists and is a taxonomy field. - if (!($field = field_info_field($field_name)) || $field['type'] !== 'taxonomy_term_reference') { + if (!($field = $this->fieldInfo->getField($field_name)) || $field['type'] !== 'taxonomy_term_reference') { // Error string. The JavaScript handler will realize this is not JSON and // will display it as debugging information. return new Response(t('Taxonomy field @field_name not found.', array('@field_name' => $field_name)), 403); @@ -104,31 +128,31 @@ public function autocomplete(Request $request, $field_name) { $vids[] = $tree['vocabulary']; } - $query = $this->database->select('taxonomy_term_data', 't'); + $query = $this->entityQuery->get('taxonomy_term'); $query->addTag('term_access'); // Do not select already entered terms. if (!empty($tags_typed)) { - $query->condition('t.name', $tags_typed, 'NOT IN'); + $query->condition('name', $tags_typed, 'NOT IN'); } // Select rows that match by term name. - $tags_return = $query - ->fields('t', array('tid', 'name')) - ->condition('t.vid', $vids) - ->condition('t.name', '%' . db_like($tag_last) . '%', 'LIKE') + $tids = $query + ->condition('vid', $vids, 'IN') + ->condition('name', $tag_last, 'CONTAINS') ->range(0, 10) - ->execute() - ->fetchAllKeyed(); - - $prefix = count($tags_typed) ? drupal_implode_tags($tags_typed) . ', ' : ''; - - foreach ($tags_return as $tid => $name) { - $n = $name; - // Term names containing commas or quotes must be wrapped in quotes. - if (strpos($name, ',') !== FALSE || strpos($name, '"') !== FALSE) { - $n = '"' . str_replace('"', '""', $name) . '"'; + ->execute(); + + $prefix = count($tags_typed) ? Tags::implode($tags_typed) . ', ' : ''; + if (!empty($tids)) { + $terms = $this->termStorageController->loadMultiple(array_keys($tids)); + foreach ($terms as $term) { + $name = $term->label(); + // Term names containing commas or quotes must be wrapped in quotes. + if (strpos($name, ',') !== FALSE || strpos($name, '"') !== FALSE) { + $name = '"' . str_replace('"', '""', $name) . '"'; + } + $matches[$prefix . $name] = String::checkPlain($term->label()); } - $matches[$prefix . $n] = String::checkPlain($name); } }