You are here

class CategoryAutocompleteController in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/block/src/Controller/CategoryAutocompleteController.php \Drupal\block\Controller\CategoryAutocompleteController

Returns autocomplete responses for block categories.

Hierarchy

Expanded class hierarchy of CategoryAutocompleteController

1 file declares its use of CategoryAutocompleteController
CategoryAutocompleteTest.php in core/modules/block/tests/src/Unit/CategoryAutocompleteTest.php
Contains \Drupal\Tests\block\Unit\CategoryAutocompleteTest.

File

core/modules/block/src/Controller/CategoryAutocompleteController.php, line 20
Contains \Drupal\block\Controller\CategoryAutocompleteController.

Namespace

Drupal\block\Controller
View source
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);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CategoryAutocompleteController::$blockManager protected property The block manager.
CategoryAutocompleteController::autocomplete public function Retrieves suggestions for block category autocompletion.
CategoryAutocompleteController::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
CategoryAutocompleteController::__construct public function Constructs a new CategoryAutocompleteController.