Pull merge.
[yaffs-website] / web / core / modules / shortcut / src / ShortcutForm.php
1 <?php
2
3 namespace Drupal\shortcut;
4
5 use Drupal\Core\Entity\ContentEntityForm;
6 use Drupal\Core\Form\FormStateInterface;
7
8 /**
9  * Form handler for the shortcut entity forms.
10  *
11  * @internal
12  */
13 class ShortcutForm extends ContentEntityForm {
14
15   /**
16    * The entity being used by this form.
17    *
18    * @var \Drupal\shortcut\ShortcutInterface
19    */
20   protected $entity;
21
22   /**
23    * {@inheritdoc}
24    */
25   public function form(array $form, FormStateInterface $form_state) {
26     $form = parent::form($form, $form_state);
27     $form['#attached']['library'][] = 'core/drupal.form';
28
29     return $form;
30   }
31
32   /**
33    * {@inheritdoc}
34    */
35   public function save(array $form, FormStateInterface $form_state) {
36     $entity = $this->entity;
37     $status = $entity->save();
38     $url = $entity->getUrl();
39     // There's an edge case where a user can have permission to
40     // 'link to any content', but has no right to access the linked page. So we
41     // check the access before showing the link.
42     if ($url->access()) {
43       $view_link = \Drupal::l($entity->getTitle(), $url);
44     }
45     else {
46       $view_link = $entity->getTitle();
47     }
48
49     if ($status == SAVED_UPDATED) {
50       $message = $this->t('The shortcut %link has been updated.', ['%link' => $view_link]);
51     }
52     else {
53       $message = $this->t('Added a shortcut for %title.', ['%title' => $view_link]);
54     }
55     $this->messenger()->addStatus($message);
56
57     $form_state->setRedirect(
58       'entity.shortcut_set.customize_form',
59       ['shortcut_set' => $entity->bundle()]
60     );
61   }
62
63 }