1cebd9091c2f41af3bb4ce834e0780b0778d6ae3
[yaffs-website] / web / core / modules / menu_ui / src / Form / MenuDeleteForm.php
1 <?php
2
3 namespace Drupal\menu_ui\Form;
4
5 use Drupal\Core\Database\Connection;
6 use Drupal\Core\Entity\EntityDeleteForm;
7 use Drupal\Core\Menu\MenuLinkManagerInterface;
8 use Drupal\Core\Form\FormStateInterface;
9 use Symfony\Component\DependencyInjection\ContainerInterface;
10
11 /**
12  * Defines a confirmation form for deletion of a custom menu.
13  */
14 class MenuDeleteForm extends EntityDeleteForm {
15
16   /**
17    * The menu link manager.
18    *
19    * @var \Drupal\Core\Menu\MenuLinkManagerInterface
20    */
21   protected $menuLinkManager;
22
23   /**
24    * The database connection.
25    *
26    * @var \Drupal\Core\Database\Connection
27    */
28   protected $connection;
29
30   /**
31    * Constructs a new MenuDeleteForm.
32    *
33    * @param \Drupal\Core\Menu\MenuLinkManagerInterface $menu_link_manager
34    *   The menu link manager.
35    * @param \Drupal\Core\Database\Connection $connection
36    *   The database connection.
37    */
38   public function __construct(MenuLinkManagerInterface $menu_link_manager, Connection $connection) {
39     $this->menuLinkManager = $menu_link_manager;
40     $this->connection = $connection;
41   }
42
43   /**
44    * {@inheritdoc}
45    */
46   public static function create(ContainerInterface $container) {
47     return new static(
48       $container->get('plugin.manager.menu.link'),
49       $container->get('database')
50     );
51   }
52
53   /**
54    * {@inheritdoc}
55    */
56   public function getDescription() {
57     $caption = '';
58     $num_links = $this->menuLinkManager->countMenuLinks($this->entity->id());
59     if ($num_links) {
60       $caption .= '<p>' . $this->formatPlural($num_links, '<strong>Warning:</strong> There is currently 1 menu link in %title. It will be deleted (system-defined items will be reset).', '<strong>Warning:</strong> There are currently @count menu links in %title. They will be deleted (system-defined links will be reset).', ['%title' => $this->entity->label()]) . '</p>';
61     }
62     $caption .= '<p>' . t('This action cannot be undone.') . '</p>';
63     return $caption;
64   }
65
66   /**
67    * {@inheritdoc}
68    */
69   protected function logDeletionMessage() {
70     $this->logger('menu')->notice('Deleted custom menu %title and all its menu links.', ['%title' => $this->entity->label()]);
71   }
72
73   /**
74    * {@inheritdoc}
75    */
76   public function submitForm(array &$form, FormStateInterface $form_state) {
77     // Locked menus may not be deleted.
78     if ($this->entity->isLocked()) {
79       return;
80     }
81
82     // Delete all links to the overview page for this menu.
83     // @todo Add a more generic helper function to the menu link plugin
84     //   manager to remove links to a entity or other ID used as a route
85     //   parameter that is being removed. Also, consider moving this to
86     //   menu_ui.module as part of a generic response to entity deletion.
87     //   https://www.drupal.org/node/2310329
88     $menu_links = $this->menuLinkManager->loadLinksByRoute('entity.menu.edit_form', ['menu' => $this->entity->id()], TRUE);
89     foreach ($menu_links as $id => $link) {
90       $this->menuLinkManager->removeDefinition($id);
91     }
92
93     parent::submitForm($form, $form_state);
94   }
95
96 }