Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
0.00% |
0 / 1 |
|
66.67% |
2 / 3 |
CRAP | |
80.00% |
8 / 10 |
| CategoryAutocompleteController | |
0.00% |
0 / 1 |
|
66.67% |
2 / 3 |
5.20 | |
80.00% |
8 / 10 |
| __construct | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
| create | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 2 |
|||
| autocomplete | |
100.00% |
1 / 1 |
3 | |
100.00% |
6 / 6 |
|||
| <?php | |
| /** | |
| * @file | |
| * Contains \Drupal\block\Controller\CategoryAutocompleteController. | |
| */ | |
| namespace Drupal\block\Controller; | |
| use Drupal\Component\Utility\Html; | |
| use Drupal\Core\Block\BlockManagerInterface; | |
| use Drupal\Core\DependencyInjection\ContainerInjectionInterface; | |
| use Symfony\Component\DependencyInjection\ContainerInterface; | |
| use Symfony\Component\HttpFoundation\JsonResponse; | |
| use Symfony\Component\HttpFoundation\Request; | |
| /** | |
| * Returns autocomplete responses for block categories. | |
| */ | |
| class CategoryAutocompleteController implements ContainerInjectionInterface { | |
| /** | |
| * The block manager. | |
| * | |
| * @var \Drupal\Core\Block\BlockManagerInterface | |
| */ | |
| protected $blockManager; | |
| /** | |
| * Constructs a new CategoryAutocompleteController. | |
| * | |
| * @param \Drupal\Core\Block\BlockManagerInterface $block_manager | |
| * The block manager. | |
| */ | |
| public function __construct(BlockManagerInterface $block_manager) { | |
| $this->blockManager = $block_manager; | |
| } | |
| /** | |
| * {@inheritdoc} | |
| */ | |
| public static function create(ContainerInterface $container) { | |
| return new static( | |
| $container->get('plugin.manager.block') | |
| ); | |
| } | |
| /** | |
| * Retrieves suggestions for block category autocompletion. | |
| * | |
| * @param \Symfony\Component\HttpFoundation\Request $request | |
| * The current request. | |
| * | |
| * @return \Symfony\Component\HttpFoundation\JsonResponse | |
| * A JSON response containing autocomplete suggestions. | |
| */ | |
| public function autocomplete(Request $request) { | |
| $typed_category = $request->query->get('q'); | |
| $matches = array(); | |
| foreach ($this->blockManager->getCategories() as $category) { | |
| if (stripos($category, $typed_category) === 0) { | |
| $matches[] = array('value' => $category, 'label' => Html::escape($category)); | |
| } | |
| } | |
| return new JsonResponse($matches); | |
| } | |
| } |