2dbd6a5e8adb5ec94ad0d81075e8f3793bd26b70
[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  * @see \Drupal\Core\Menu\MenuLinkInterface::getFormClass()
17  */
18 class MenuLinkEditForm extends FormBase {
19
20   /**
21    * The class resolver.
22    *
23    * @var \Drupal\Core\DependencyInjection\ClassResolverInterface
24    */
25   protected $classResolver;
26
27   /**
28    * Constructs a MenuLinkEditForm object.
29    *
30    * @param \Drupal\Core\DependencyInjection\ClassResolverInterface $class_resolver
31    *   The class resolver.
32    */
33   public function __construct(ClassResolverInterface $class_resolver) {
34     $this->classResolver = $class_resolver;
35   }
36
37   /**
38    * {@inheritdoc}
39    */
40   public static function create(ContainerInterface $container) {
41     return new static(
42       $container->get('class_resolver')
43     );
44   }
45
46   /**
47    * {@inheritdoc}
48    */
49   public function getFormId() {
50     return 'menu_link_edit';
51   }
52
53   /**
54    * {@inheritdoc}
55    *
56    * @param \Drupal\Core\Menu\MenuLinkInterface $menu_link_plugin
57    *   The plugin instance to use for this form.
58    */
59   public function buildForm(array $form, FormStateInterface $form_state, MenuLinkInterface $menu_link_plugin = NULL) {
60     $form['menu_link_id'] = [
61       '#type' => 'value',
62       '#value' => $menu_link_plugin->getPluginId(),
63     ];
64     $class_name = $menu_link_plugin->getFormClass();
65     $form['#plugin_form'] = $this->classResolver->getInstanceFromDefinition($class_name);
66     $form['#plugin_form']->setMenuLinkInstance($menu_link_plugin);
67
68     $form += $form['#plugin_form']->buildConfigurationForm($form, $form_state);
69
70     $form['actions'] = ['#type' => 'actions'];
71     $form['actions']['submit'] = [
72       '#type' => 'submit',
73       '#value' => $this->t('Save'),
74       '#button_type' => 'primary',
75     ];
76     return $form;
77   }
78
79   /**
80    * {@inheritdoc}
81    */
82   public function validateForm(array &$form, FormStateInterface $form_state) {
83     $form['#plugin_form']->validateConfigurationForm($form, $form_state);
84   }
85
86   /**
87    * {@inheritdoc}
88    */
89   public function submitForm(array &$form, FormStateInterface $form_state) {
90     $link = $form['#plugin_form']->submitConfigurationForm($form, $form_state);
91
92     drupal_set_message($this->t('The menu link has been saved.'));
93     $form_state->setRedirect(
94       'entity.menu.edit_form',
95       ['menu' => $link->getMenuName()]
96     );
97   }
98
99 }