You are here

public function ShortcutSetController::addShortcutLinkInline in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/shortcut/src/Controller/ShortcutSetController.php \Drupal\shortcut\Controller\ShortcutSetController::addShortcutLinkInline()
  2. 9 core/modules/shortcut/src/Controller/ShortcutSetController.php \Drupal\shortcut\Controller\ShortcutSetController::addShortcutLinkInline()

Creates a new link in the provided shortcut set.

Parameters

\Drupal\shortcut\ShortcutSetInterface $shortcut_set: The shortcut set to add a link to.

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

Return value

\Symfony\Component\HttpFoundation\RedirectResponse A redirect response to the front page, or the previous location.

Throws

\Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException

1 string reference to 'ShortcutSetController::addShortcutLinkInline'
shortcut.routing.yml in core/modules/shortcut/shortcut.routing.yml
core/modules/shortcut/shortcut.routing.yml

File

core/modules/shortcut/src/Controller/ShortcutSetController.php, line 54

Class

ShortcutSetController
Builds the page for administering shortcut sets.

Namespace

Drupal\shortcut\Controller

Code

public function addShortcutLinkInline(ShortcutSetInterface $shortcut_set, Request $request) {
  $link = $request->query
    ->get('link');
  $name = $request->query
    ->get('name');
  if (parse_url($link, PHP_URL_SCHEME) === NULL && $this->pathValidator
    ->isValid($link)) {
    $shortcut = $this
      ->entityTypeManager()
      ->getStorage('shortcut')
      ->create([
      'title' => $name,
      'shortcut_set' => $shortcut_set
        ->id(),
      'link' => [
        'uri' => 'internal:/' . $link,
      ],
    ]);
    try {
      $shortcut
        ->save();
      $this
        ->messenger()
        ->addStatus($this
        ->t('Added a shortcut for %title.', [
        '%title' => $shortcut
          ->label(),
      ]));
    } catch (\Exception $e) {
      $this
        ->messenger()
        ->addError($this
        ->t('Unable to add a shortcut for %title.', [
        '%title' => $shortcut
          ->label(),
      ]));
    }
    return $this
      ->redirect('<front>');
  }
  throw new AccessDeniedHttpException();
}