4bdd6b9cfe0300dd9f7fe530a5694e752672a362
[yaffs-website] / web / core / modules / menu_link_content / src / Form / MenuLinkContentForm.php
1 <?php
2
3 namespace Drupal\menu_link_content\Form;
4
5 use Drupal\Component\Datetime\TimeInterface;
6 use Drupal\Core\Entity\ContentEntityForm;
7 use Drupal\Core\Entity\EntityManagerInterface;
8 use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
9 use Drupal\Core\Form\FormStateInterface;
10 use Drupal\Core\Language\LanguageManagerInterface;
11 use Drupal\Core\Menu\MenuParentFormSelectorInterface;
12 use Drupal\Core\Path\PathValidatorInterface;
13 use Symfony\Component\DependencyInjection\ContainerInterface;
14
15 /**
16  * Provides a form to add/update content menu links.
17  */
18 class MenuLinkContentForm extends ContentEntityForm {
19
20   /**
21    * The content menu link.
22    *
23    * @var \Drupal\menu_link_content\MenuLinkContentInterface
24    */
25   protected $entity;
26
27   /**
28    * The parent form selector service.
29    *
30    * @var \Drupal\Core\Menu\MenuParentFormSelectorInterface
31    */
32   protected $menuParentSelector;
33
34   /**
35    * The path validator.
36    *
37    * @var \Drupal\Core\Path\PathValidatorInterface
38    */
39   protected $pathValidator;
40
41   /**
42    * Constructs a MenuLinkContentForm object.
43    *
44    * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
45    *   The entity manager.
46    * @param \Drupal\Core\Menu\MenuParentFormSelectorInterface $menu_parent_selector
47    *   The menu parent form selector service.
48    * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
49    *   The language manager.
50    * @param \Drupal\Core\Path\PathValidatorInterface $path_validator
51    *   The path validator.
52    * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
53    *   The entity type bundle service.
54    * @param \Drupal\Component\Datetime\TimeInterface $time
55    *   The time service.
56    */
57   public function __construct(EntityManagerInterface $entity_manager, MenuParentFormSelectorInterface $menu_parent_selector, LanguageManagerInterface $language_manager, PathValidatorInterface $path_validator, EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL, TimeInterface $time = NULL) {
58     parent::__construct($entity_manager, $entity_type_bundle_info, $time);
59     $this->menuParentSelector = $menu_parent_selector;
60     $this->pathValidator = $path_validator;
61   }
62
63   /**
64    * {@inheritdoc}
65    */
66   public static function create(ContainerInterface $container) {
67     return new static(
68       $container->get('entity.manager'),
69       $container->get('menu.parent_form_selector'),
70       $container->get('language_manager'),
71       $container->get('path.validator'),
72       $container->get('entity_type.bundle.info'),
73       $container->get('datetime.time')
74     );
75   }
76
77   /**
78    * {@inheritdoc}
79    */
80   public function form(array $form, FormStateInterface $form_state) {
81     $form = parent::form($form, $form_state);
82
83     $default = $this->entity->getMenuName() . ':' . $this->entity->getParentId();
84     $id = $this->entity->isNew() ? '' : $this->entity->getPluginId();
85     $form['menu_parent'] = $this->menuParentSelector->parentSelectElement($default, $id);
86     $form['menu_parent']['#weight'] = 10;
87     $form['menu_parent']['#title'] = $this->t('Parent link');
88     $form['menu_parent']['#description'] = $this->t('The maximum depth for a link and all its children is fixed. Some menu links may not be available as parents if selecting them would exceed this limit.');
89     $form['menu_parent']['#attributes']['class'][] = 'menu-title-select';
90
91     return $form;
92   }
93
94   /**
95    * {@inheritdoc}
96    */
97   protected function actions(array $form, FormStateInterface $form_state) {
98     $element = parent::actions($form, $form_state);
99     $element['submit']['#button_type'] = 'primary';
100     $element['delete']['#access'] = $this->entity->access('delete');
101
102     return $element;
103   }
104
105   /**
106    * {@inheritdoc}
107    */
108   public function buildEntity(array $form, FormStateInterface $form_state) {
109     /** @var \Drupal\menu_link_content\MenuLinkContentInterface $entity */
110     $entity = parent::buildEntity($form, $form_state);
111
112     list($menu_name, $parent) = explode(':', $form_state->getValue('menu_parent'), 2);
113
114     $entity->parent->value = $parent;
115     $entity->menu_name->value = $menu_name;
116     $entity->enabled->value = (!$form_state->isValueEmpty(['enabled', 'value']));
117     $entity->expanded->value = (!$form_state->isValueEmpty(['expanded', 'value']));
118
119     return $entity;
120   }
121
122   /**
123    * {@inheritdoc}
124    */
125   public function save(array $form, FormStateInterface $form_state) {
126     // The entity is rebuilt in parent::submit().
127     $menu_link = $this->entity;
128     $saved = $menu_link->save();
129
130     if ($saved) {
131       drupal_set_message($this->t('The menu link has been saved.'));
132       $form_state->setRedirect(
133         'entity.menu_link_content.canonical',
134         ['menu_link_content' => $menu_link->id()]
135       );
136     }
137     else {
138       drupal_set_message($this->t('There was an error saving the menu link.'), 'error');
139       $form_state->setRebuild();
140     }
141   }
142
143 }