Updated Drupal to 8.6. This goes with the following updates because it's possible...
[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\EntityRepositoryInterface;
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  * @internal
19  */
20 class MenuLinkContentForm extends ContentEntityForm {
21
22   /**
23    * The content menu link.
24    *
25    * @var \Drupal\menu_link_content\MenuLinkContentInterface
26    */
27   protected $entity;
28
29   /**
30    * The parent form selector service.
31    *
32    * @var \Drupal\Core\Menu\MenuParentFormSelectorInterface
33    */
34   protected $menuParentSelector;
35
36   /**
37    * The path validator.
38    *
39    * @var \Drupal\Core\Path\PathValidatorInterface
40    */
41   protected $pathValidator;
42
43   /**
44    * Constructs a MenuLinkContentForm object.
45    *
46    * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
47    *   The entity repository.
48    * @param \Drupal\Core\Menu\MenuParentFormSelectorInterface $menu_parent_selector
49    *   The menu parent form selector service.
50    * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
51    *   The language manager.
52    * @param \Drupal\Core\Path\PathValidatorInterface $path_validator
53    *   The path validator.
54    * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
55    *   The entity type bundle service.
56    * @param \Drupal\Component\Datetime\TimeInterface $time
57    *   The time service.
58    */
59   public function __construct(EntityRepositoryInterface $entity_repository, MenuParentFormSelectorInterface $menu_parent_selector, LanguageManagerInterface $language_manager, PathValidatorInterface $path_validator, EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL, TimeInterface $time = NULL) {
60     parent::__construct($entity_repository, $entity_type_bundle_info, $time);
61     $this->menuParentSelector = $menu_parent_selector;
62     $this->pathValidator = $path_validator;
63   }
64
65   /**
66    * {@inheritdoc}
67    */
68   public static function create(ContainerInterface $container) {
69     return new static(
70       $container->get('entity.repository'),
71       $container->get('menu.parent_form_selector'),
72       $container->get('language_manager'),
73       $container->get('path.validator'),
74       $container->get('entity_type.bundle.info'),
75       $container->get('datetime.time')
76     );
77   }
78
79   /**
80    * {@inheritdoc}
81    */
82   public function form(array $form, FormStateInterface $form_state) {
83     $form = parent::form($form, $form_state);
84
85     $default = $this->entity->getMenuName() . ':' . $this->entity->getParentId();
86     $id = $this->entity->isNew() ? '' : $this->entity->getPluginId();
87     $form['menu_parent'] = $this->menuParentSelector->parentSelectElement($default, $id);
88     $form['menu_parent']['#weight'] = 10;
89     $form['menu_parent']['#title'] = $this->t('Parent link');
90     $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.');
91     $form['menu_parent']['#attributes']['class'][] = 'menu-title-select';
92
93     return $form;
94   }
95
96   /**
97    * {@inheritdoc}
98    */
99   protected function actions(array $form, FormStateInterface $form_state) {
100     $element = parent::actions($form, $form_state);
101     $element['submit']['#button_type'] = 'primary';
102     $element['delete']['#access'] = $this->entity->access('delete');
103
104     return $element;
105   }
106
107   /**
108    * {@inheritdoc}
109    */
110   public function buildEntity(array $form, FormStateInterface $form_state) {
111     /** @var \Drupal\menu_link_content\MenuLinkContentInterface $entity */
112     $entity = parent::buildEntity($form, $form_state);
113
114     list($menu_name, $parent) = explode(':', $form_state->getValue('menu_parent'), 2);
115
116     $entity->parent->value = $parent;
117     $entity->menu_name->value = $menu_name;
118     $entity->enabled->value = (!$form_state->isValueEmpty(['enabled', 'value']));
119     $entity->expanded->value = (!$form_state->isValueEmpty(['expanded', 'value']));
120
121     return $entity;
122   }
123
124   /**
125    * {@inheritdoc}
126    */
127   public function save(array $form, FormStateInterface $form_state) {
128     // The entity is rebuilt in parent::submit().
129     $menu_link = $this->entity;
130     $saved = $menu_link->save();
131
132     if ($saved) {
133       $this->messenger()->addStatus($this->t('The menu link has been saved.'));
134       $form_state->setRedirect(
135         'entity.menu_link_content.canonical',
136         ['menu_link_content' => $menu_link->id()]
137       );
138     }
139     else {
140       $this->messenger()->addError($this->t('There was an error saving the menu link.'));
141       $form_state->setRebuild();
142     }
143   }
144
145 }