3 namespace Drupal\shortcut\Controller;
5 use Drupal\Core\Controller\ControllerBase;
6 use Drupal\Core\Path\PathValidatorInterface;
7 use Drupal\shortcut\ShortcutSetInterface;
8 use Symfony\Component\DependencyInjection\ContainerInterface;
9 use Symfony\Component\HttpFoundation\Request;
10 use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
13 * Builds the page for administering shortcut sets.
15 class ShortcutSetController extends ControllerBase {
20 * @var \Drupal\Core\Path\PathValidatorInterface
22 protected $pathValidator;
25 * Creates a new ShortcutSetController instance.
27 * @param \Drupal\Core\Path\PathValidatorInterface $path_validator
30 public function __construct(PathValidatorInterface $path_validator) {
31 $this->pathValidator = $path_validator;
37 public static function create(ContainerInterface $container) {
38 return new static($container->get('path.validator'));
42 * Creates a new link in the provided shortcut set.
44 * @param \Drupal\shortcut\ShortcutSetInterface $shortcut_set
45 * The shortcut set to add a link to.
46 * @param \Symfony\Component\HttpFoundation\Request $request
49 * @return \Symfony\Component\HttpFoundation\RedirectResponse
50 * A redirect response to the front page, or the previous location.
52 * @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
54 public function addShortcutLinkInline(ShortcutSetInterface $shortcut_set, Request $request) {
55 $link = $request->query->get('link');
56 $name = $request->query->get('name');
57 if (parse_url($link, PHP_URL_SCHEME) === NULL && $this->pathValidator->isValid($link)) {
58 $shortcut = $this->entityManager()->getStorage('shortcut')->create([
60 'shortcut_set' => $shortcut_set->id(),
62 'uri' => 'internal:/' . $link,
68 $this->messenger()->addStatus($this->t('Added a shortcut for %title.', ['%title' => $shortcut->label()]));
70 catch (\Exception $e) {
71 $this->messenger()->addError($this->t('Unable to add a shortcut for %title.', ['%title' => $shortcut->label()]));
74 return $this->redirect('<front>');
77 throw new AccessDeniedHttpException();