You are here

public function LinkController::add in Colossal Menu 8

Same name and namespace in other branches
  1. 2.x src/Controller/LinkController.php \Drupal\colossal_menu\Controller\LinkController::add()

Add new Link page.

Displays add links for available bundles/types for entity colossal_menu_link.

Parameters

\Drupal\system\MenuInterface $colossal_menu: An entity representing a custom menu.

\Symfony\Component\HttpFoundation\Request $request: The current request object.

Return value

array A render array for a list of the colossal_menu_link bundles/types that can be added or if there is only one type/bunlde defined for the site, the function returns the add page for that bundle/type.

1 string reference to 'LinkController::add'
colossal_menu.routing.yml in ./colossal_menu.routing.yml
colossal_menu.routing.yml

File

src/Controller/LinkController.php, line 93

Class

LinkController
Class LinkAddController.

Namespace

Drupal\colossal_menu\Controller

Code

public function add(MenuInterface $colossal_menu, Request $request) {
  $types = $this->typeStorage
    ->loadMultiple();
  if ($types && count($types) == 1) {
    $type = reset($types);
    return $this
      ->addForm($colossal_menu, $type, $request);
  }
  if (count($types) === 0) {
    return [
      '#markup' => $this
        ->t('You have not created any %bundle types yet. @link to add a new type.', [
        '%bundle' => 'Link',
        '@link' => Link::fromTextAndUrl($this
          ->t('Go to the type creation page'), Url::fromRoute('entity.colossal_menu_link_type.add_form')),
      ]),
    ];
  }
  $links = [];
  foreach ($types as $type) {
    $params = [
      'colossal_menu_link_type' => $type
        ->id(),
      'colossal_menu' => $this->routeMatch
        ->getParameter('colossal_menu')
        ->id(),
    ];
    $options = [
      'query' => $request->query
        ->all(),
    ];
    $url = new Url('entity.colossal_menu_link.add_form', $params, $options);
    $links[$type
      ->id()] = [
      'url' => $url,
      'title' => $type
        ->label(),
    ];
  }
  return [
    '#theme' => 'links',
    '#links' => $links,
    '#attributes' => [
      'class' => [
        'admin-list',
      ],
    ],
  ];
}