3 namespace Drupal\menu_ui\Form;
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;
12 * Defines a confirmation form for deletion of a custom menu.
14 class MenuDeleteForm extends EntityDeleteForm {
17 * The menu link manager.
19 * @var \Drupal\Core\Menu\MenuLinkManagerInterface
21 protected $menuLinkManager;
24 * The database connection.
26 * @var \Drupal\Core\Database\Connection
28 protected $connection;
31 * Constructs a new MenuDeleteForm.
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.
38 public function __construct(MenuLinkManagerInterface $menu_link_manager, Connection $connection) {
39 $this->menuLinkManager = $menu_link_manager;
40 $this->connection = $connection;
46 public static function create(ContainerInterface $container) {
48 $container->get('plugin.manager.menu.link'),
49 $container->get('database')
56 public function getDescription() {
58 $num_links = $this->menuLinkManager->countMenuLinks($this->entity->id());
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>';
62 $caption .= '<p>' . t('This action cannot be undone.') . '</p>';
69 protected function logDeletionMessage() {
70 $this->logger('menu')->notice('Deleted custom menu %title and all its menu links.', ['%title' => $this->entity->label()]);
76 public function submitForm(array &$form, FormStateInterface $form_state) {
77 // Locked menus may not be deleted.
78 if ($this->entity->isLocked()) {
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);
93 parent::submitForm($form, $form_state);