Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / shortcut / src / Controller / ShortcutSetController.php
1 <?php
2
3 namespace Drupal\shortcut\Controller;
4
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;
11
12 /**
13  * Builds the page for administering shortcut sets.
14  */
15 class ShortcutSetController extends ControllerBase {
16
17   /**
18    * The path validator.
19    *
20    * @var \Drupal\Core\Path\PathValidatorInterface
21    */
22   protected $pathValidator;
23
24   /**
25    * Creates a new ShortcutSetController instance.
26    *
27    * @param \Drupal\Core\Path\PathValidatorInterface $path_validator
28    *   The path validator.
29    */
30   public function __construct(PathValidatorInterface $path_validator) {
31     $this->pathValidator = $path_validator;
32   }
33
34   /**
35    * {@inheritdoc}
36    */
37   public static function create(ContainerInterface $container) {
38     return new static($container->get('path.validator'));
39   }
40
41   /**
42    * Creates a new link in the provided shortcut set.
43    *
44    * @param \Drupal\shortcut\ShortcutSetInterface $shortcut_set
45    *   The shortcut set to add a link to.
46    * @param \Symfony\Component\HttpFoundation\Request $request
47    *   The request object.
48    *
49    * @return \Symfony\Component\HttpFoundation\RedirectResponse
50    *   A redirect response to the front page, or the previous location.
51    *
52    * @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
53    */
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([
59         'title' => $name,
60         'shortcut_set' => $shortcut_set->id(),
61         'link' => [
62           'uri' => 'internal:/' . $link,
63         ],
64       ]);
65
66       try {
67         $shortcut->save();
68         $this->messenger()->addStatus($this->t('Added a shortcut for %title.', ['%title' => $shortcut->label()]));
69       }
70       catch (\Exception $e) {
71         $this->messenger()->addError($this->t('Unable to add a shortcut for %title.', ['%title' => $shortcut->label()]));
72       }
73
74       return $this->redirect('<front>');
75     }
76
77     throw new AccessDeniedHttpException();
78   }
79
80 }