e37b16b0721703d9c5d2405d6c4c19e239dd5491
[yaffs-website] / web / core / modules / menu_ui / src / Form / MenuLinkResetForm.php
1 <?php
2
3 namespace Drupal\menu_ui\Form;
4
5 use Drupal\Core\Access\AccessResult;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\Core\Url;
8 use Drupal\Core\Form\ConfirmFormBase;
9 use Drupal\Core\Menu\MenuLinkManagerInterface;
10 use Drupal\Core\Menu\MenuLinkInterface;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
12
13 /**
14  * Defines a confirmation form for resetting a single modified menu link.
15  */
16 class MenuLinkResetForm extends ConfirmFormBase {
17
18   /**
19    * The menu link manager.
20    *
21    * @var \Drupal\Core\Menu\MenuLinkManagerInterface
22    */
23   protected $menuLinkManager;
24
25   /**
26    * The menu link.
27    *
28    * @var \Drupal\Core\Menu\MenuLinkInterface
29    */
30   protected $link;
31
32   /**
33    * Constructs a MenuLinkResetForm object.
34    *
35    * @param \Drupal\Core\Menu\MenuLinkManagerInterface $menu_link_manager
36    *   The menu link manager.
37    */
38   public function __construct(MenuLinkManagerInterface $menu_link_manager) {
39     $this->menuLinkManager = $menu_link_manager;
40   }
41
42   /**
43    * {@inheritdoc}
44    */
45   public static function create(ContainerInterface $container) {
46     return new static(
47       $container->get('plugin.manager.menu.link')
48     );
49   }
50
51   /**
52    * {@inheritdoc}
53    */
54   public function getFormId() {
55     return 'menu_link_reset_confirm';
56   }
57
58   /**
59    * {@inheritdoc}
60    */
61   public function getQuestion() {
62     return $this->t('Are you sure you want to reset the link %item to its default values?', ['%item' => $this->link->getTitle()]);
63   }
64
65   /**
66    * {@inheritdoc}
67    */
68   public function getCancelUrl() {
69     return new Url('entity.menu.edit_form', [
70       'menu' => $this->link->getMenuName(),
71     ]);
72   }
73
74   /**
75    * {@inheritdoc}
76    */
77   public function getDescription() {
78     return $this->t('Any customizations will be lost. This action cannot be undone.');
79   }
80
81   /**
82    * {@inheritdoc}
83    */
84   public function getConfirmText() {
85     return $this->t('Reset');
86   }
87
88   /**
89    * {@inheritdoc}
90    */
91   public function buildForm(array $form, FormStateInterface $form_state, MenuLinkInterface $menu_link_plugin = NULL) {
92     $this->link = $menu_link_plugin;
93
94     $form = parent::buildForm($form, $form_state);
95     return $form;
96   }
97
98   /**
99    * {@inheritdoc}
100    */
101   public function submitForm(array &$form, FormStateInterface $form_state) {
102     $this->link = $this->menuLinkManager->resetLink($this->link->getPluginId());
103     drupal_set_message($this->t('The menu link was reset to its default settings.'));
104     $form_state->setRedirectUrl($this->getCancelUrl());
105   }
106
107   /**
108    * Checks access based on whether the link can be reset.
109    *
110    * @param \Drupal\Core\Menu\MenuLinkInterface $menu_link_plugin
111    *   The menu link plugin being checked.
112    *
113    * @return \Drupal\Core\Access\AccessResultInterface
114    *   The access result.
115    */
116   public function linkIsResettable(MenuLinkInterface $menu_link_plugin) {
117     return AccessResult::allowedIf($menu_link_plugin->isResettable())->setCacheMaxAge(0);
118   }
119
120 }