You are here

public function CoffeeController::coffeeData in Coffee 8

Outputs the data that is used for the Coffee autocompletion in JSON.

Return value

\Symfony\Component\HttpFoundation\JsonResponse The json response.

1 string reference to 'CoffeeController::coffeeData'
coffee.routing.yml in ./coffee.routing.yml
coffee.routing.yml

File

src/Controller/CoffeeController.php, line 105

Class

CoffeeController
Provides route responses for coffee.module.

Namespace

Drupal\coffee\Controller

Code

public function coffeeData() {
  $output = [];
  foreach ($this->config
    ->get('coffee_menus') as $menu_name) {
    $tree = $this
      ->getMenuTreeElements($menu_name);
    $commands_group = $menu_name == 'account' ? ':user' : NULL;
    foreach ($tree as $tree_element) {
      $link = $tree_element->link;
      $output[$link
        ->getRouteName()] = [
        'value' => $link
          ->getUrlObject()
          ->setUrlGenerator($this->urlGenerator)
          ->toString(),
        'label' => $link
          ->getTitle(),
        'command' => $commands_group,
      ];
      $tasks = $this
        ->getLocalTasksForRoute($link
        ->getRouteName(), $link
        ->getRouteParameters());
      foreach ($tasks as $route_name => $task) {
        if (empty($output[$route_name])) {
          $output[$route_name] = [
            'value' => $task['url']
              ->setUrlGenerator($this->urlGenerator)
              ->toString(),
            'label' => $link
              ->getTitle() . ' - ' . $task['title'],
            'command' => NULL,
          ];
        }
      }
    }
  }
  $commands = $this
    ->moduleHandler()
    ->invokeAll('coffee_commands');
  if (!empty($commands)) {
    $output = array_merge($output, $commands);
  }

  // Re-index the array.
  $output = array_values($output);
  return new JsonResponse($output);
}