Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / menu_ui / src / Form / MenuLinkEditForm.php
1 <?php
2
3 namespace Drupal\menu_ui\Form;
4
5 use Drupal\Core\DependencyInjection\ClassResolverInterface;
6 use Drupal\Core\Form\FormBase;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\Core\Menu\MenuLinkInterface;
9 use Symfony\Component\DependencyInjection\ContainerInterface;
10
11 /**
12  * Defines a generic edit form for all menu link plugin types.
13  *
14  * The menu link plugin defines which class defines the corresponding form.
15  *
16  * @internal
17  *
18  * @see \Drupal\Core\Menu\MenuLinkInterface::getFormClass()
19  */
20 class MenuLinkEditForm extends FormBase {
21
22   /**
23    * The class resolver.
24    *
25    * @var \Drupal\Core\DependencyInjection\ClassResolverInterface
26    */
27   protected $classResolver;
28
29   /**
30    * Constructs a MenuLinkEditForm object.
31    *
32    * @param \Drupal\Core\DependencyInjection\ClassResolverInterface $class_resolver
33    *   The class resolver.
34    */
35   public function __construct(ClassResolverInterface $class_resolver) {
36     $this->classResolver = $class_resolver;
37   }
38
39   /**
40    * {@inheritdoc}
41    */
42   public static function create(ContainerInterface $container) {
43     return new static(
44       $container->get('class_resolver')
45     );
46   }
47
48   /**
49    * {@inheritdoc}
50    */
51   public function getFormId() {
52     return 'menu_link_edit';
53   }
54
55   /**
56    * {@inheritdoc}
57    *
58    * @param \Drupal\Core\Menu\MenuLinkInterface $menu_link_plugin
59    *   The plugin instance to use for this form.
60    */
61   public function buildForm(array $form, FormStateInterface $form_state, MenuLinkInterface $menu_link_plugin = NULL) {
62     $form['menu_link_id'] = [
63       '#type' => 'value',
64       '#value' => $menu_link_plugin->getPluginId(),
65     ];
66     $class_name = $menu_link_plugin->getFormClass();
67     $form['#plugin_form'] = $this->classResolver->getInstanceFromDefinition($class_name);
68     $form['#plugin_form']->setMenuLinkInstance($menu_link_plugin);
69
70     $form += $form['#plugin_form']->buildConfigurationForm($form, $form_state);
71
72     $form['actions'] = ['#type' => 'actions'];
73     $form['actions']['submit'] = [
74       '#type' => 'submit',
75       '#value' => $this->t('Save'),
76       '#button_type' => 'primary',
77     ];
78     return $form;
79   }
80
81   /**
82    * {@inheritdoc}
83    */
84   public function validateForm(array &$form, FormStateInterface $form_state) {
85     $form['#plugin_form']->validateConfigurationForm($form, $form_state);
86   }
87
88   /**
89    * {@inheritdoc}
90    */
91   public function submitForm(array &$form, FormStateInterface $form_state) {
92     $link = $form['#plugin_form']->submitConfigurationForm($form, $form_state);
93
94     $this->messenger()->addStatus($this->t('The menu link has been saved.'));
95     $form_state->setRedirect(
96       'entity.menu.edit_form',
97       ['menu' => $link->getMenuName()]
98     );
99   }
100
101 }