feeds_ex-8.x-1.0-alpha4/src/Controller/DefaultController.php
src/Controller/DefaultController.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | <?php namespace Drupal\feeds_ex\Controller; use Drupal\Component\Utility\Html; use Drupal\Component\Utility\Unicode; use Drupal\Core\Controller\ControllerBase; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Request; /** * Default controller for the feeds_ex module. */ class DefaultController extends ControllerBase { /** * Autocomplete callback for encodings. */ public function encodingAutocomplete(Request $request ): JsonResponse { $matches = []; $input = $request ->query->get( 'q' ); if (! strlen ( $input ) || Unicode::getStatus() != Unicode::STATUS_MULTIBYTE) { return new JsonResponse( $matches ); } $added = array_map ( 'trim' , explode ( ',' , $input )); $input = array_pop ( $added ); $lower_added = array_map ( 'mb_strtolower' , $added ); // Filter out items already added. Do it case insensitively without changing // the suggested case. $prefix = '' ; $encodings = []; foreach (mb_list_encodings() as $suggestion ) { if (in_array(mb_strtolower( $suggestion ), $lower_added )) { $prefix .= $suggestion . ', ' ; continue ; } $encodings [] = $suggestion ; } // Find starts with first. foreach ( $encodings as $delta => $encoding ) { if ( stripos ( $encoding , $input ) !== 0) { continue ; } $matches [] = [ 'value' => $prefix . $encoding , 'label' => Html::escape( $encoding ), ]; // Remove matches so we don't search them again. unset( $encodings [ $delta ]); } // Find contains next. foreach ( $encodings as $encoding ) { if ( stripos ( $encoding , $input ) !== FALSE) { $matches [] = [ 'value' => $prefix . $encoding , 'label' => Html::escape( $encoding ), ]; } } // Only send back 10 suggestions. $matches = array_slice ( $matches , 0, 10, TRUE); return new JsonResponse( $matches ); } } |