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