comparison core/modules/menu_ui/src/MenuForm.php @ 18:af1871eacc83

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:33:08 +0100
parents 129ea1e6d783
children
comparison
equal deleted inserted replaced
17:129ea1e6d783 18:af1871eacc83
13 use Drupal\Core\Menu\MenuLinkTreeInterface; 13 use Drupal\Core\Menu\MenuLinkTreeInterface;
14 use Drupal\Core\Menu\MenuTreeParameters; 14 use Drupal\Core\Menu\MenuTreeParameters;
15 use Drupal\Core\Render\Element; 15 use Drupal\Core\Render\Element;
16 use Drupal\Core\Url; 16 use Drupal\Core\Url;
17 use Drupal\Core\Utility\LinkGeneratorInterface; 17 use Drupal\Core\Utility\LinkGeneratorInterface;
18 use Drupal\menu_link_content\MenuLinkContentStorageInterface;
19 use Drupal\menu_link_content\Plugin\Menu\MenuLinkContent;
18 use Symfony\Component\DependencyInjection\ContainerInterface; 20 use Symfony\Component\DependencyInjection\ContainerInterface;
19 21
20 /** 22 /**
21 * Base form for menu edit forms. 23 * Base form for menu edit forms.
22 * 24 *
44 * @var \Drupal\Core\Utility\LinkGeneratorInterface 46 * @var \Drupal\Core\Utility\LinkGeneratorInterface
45 */ 47 */
46 protected $linkGenerator; 48 protected $linkGenerator;
47 49
48 /** 50 /**
51 * The menu_link_content storage handler.
52 *
53 * @var \Drupal\menu_link_content\MenuLinkContentStorageInterface
54 */
55 protected $menuLinkContentStorage;
56
57 /**
49 * The overview tree form. 58 * The overview tree form.
50 * 59 *
51 * @var array 60 * @var array
52 */ 61 */
53 protected $overviewTreeForm = ['#tree' => TRUE]; 62 protected $overviewTreeForm = ['#tree' => TRUE];
59 * The menu link manager. 68 * The menu link manager.
60 * @param \Drupal\Core\Menu\MenuLinkTreeInterface $menu_tree 69 * @param \Drupal\Core\Menu\MenuLinkTreeInterface $menu_tree
61 * The menu tree service. 70 * The menu tree service.
62 * @param \Drupal\Core\Utility\LinkGeneratorInterface $link_generator 71 * @param \Drupal\Core\Utility\LinkGeneratorInterface $link_generator
63 * The link generator. 72 * The link generator.
64 */ 73 * @param \Drupal\menu_link_content\MenuLinkContentStorageInterface $menu_link_content_storage
65 public function __construct(MenuLinkManagerInterface $menu_link_manager, MenuLinkTreeInterface $menu_tree, LinkGeneratorInterface $link_generator) { 74 * The menu link content storage handler.
75 */
76 public function __construct(MenuLinkManagerInterface $menu_link_manager, MenuLinkTreeInterface $menu_tree, LinkGeneratorInterface $link_generator, MenuLinkContentStorageInterface $menu_link_content_storage) {
66 $this->menuLinkManager = $menu_link_manager; 77 $this->menuLinkManager = $menu_link_manager;
67 $this->menuTree = $menu_tree; 78 $this->menuTree = $menu_tree;
68 $this->linkGenerator = $link_generator; 79 $this->linkGenerator = $link_generator;
80 $this->menuLinkContentStorage = $menu_link_content_storage;
69 } 81 }
70 82
71 /** 83 /**
72 * {@inheritdoc} 84 * {@inheritdoc}
73 */ 85 */
74 public static function create(ContainerInterface $container) { 86 public static function create(ContainerInterface $container) {
75 return new static( 87 return new static(
76 $container->get('plugin.manager.menu.link'), 88 $container->get('plugin.manager.menu.link'),
77 $container->get('menu.link_tree'), 89 $container->get('menu.link_tree'),
78 $container->get('link_generator') 90 $container->get('link_generator'),
91 $container->get('entity_type.manager')->getStorage('menu_link_content')
79 ); 92 );
80 } 93 }
81 94
82 /** 95 /**
83 * {@inheritdoc} 96 * {@inheritdoc}
162 * {@inheritdoc} 175 * {@inheritdoc}
163 */ 176 */
164 public function save(array $form, FormStateInterface $form_state) { 177 public function save(array $form, FormStateInterface $form_state) {
165 $menu = $this->entity; 178 $menu = $this->entity;
166 $status = $menu->save(); 179 $status = $menu->save();
167 $edit_link = $this->entity->link($this->t('Edit')); 180 $edit_link = $this->entity->toLink($this->t('Edit'), 'edit-form')->toString();
168 if ($status == SAVED_UPDATED) { 181 if ($status == SAVED_UPDATED) {
169 $this->messenger()->addStatus($this->t('Menu %label has been updated.', ['%label' => $menu->label()])); 182 $this->messenger()->addStatus($this->t('Menu %label has been updated.', ['%label' => $menu->label()]));
170 $this->logger('menu')->notice('Menu %label has been updated.', ['%label' => $menu->label(), 'link' => $edit_link]); 183 $this->logger('menu')->notice('Menu %label has been updated.', ['%label' => $menu->label(), 'link' => $edit_link]);
171 } 184 }
172 else { 185 else {
173 $this->messenger()->addStatus($this->t('Menu %label has been added.', ['%label' => $menu->label()])); 186 $this->messenger()->addStatus($this->t('Menu %label has been added.', ['%label' => $menu->label()]));
174 $this->logger('menu')->notice('Menu %label has been added.', ['%label' => $menu->label(), 'link' => $edit_link]); 187 $this->logger('menu')->notice('Menu %label has been added.', ['%label' => $menu->label(), 'link' => $edit_link]);
175 } 188 }
176 189
177 $form_state->setRedirectUrl($this->entity->urlInfo('edit-form')); 190 $form_state->setRedirectUrl($this->entity->toUrl('edit-form'));
178 } 191 }
179 192
180 /** 193 /**
181 * {@inheritdoc} 194 * {@inheritdoc}
182 */ 195 */
267 ], 280 ],
268 ]; 281 ];
269 282
270 $form['links']['#empty'] = $this->t('There are no menu links yet. <a href=":url">Add link</a>.', [ 283 $form['links']['#empty'] = $this->t('There are no menu links yet. <a href=":url">Add link</a>.', [
271 ':url' => $this->url('entity.menu.add_link_form', ['menu' => $this->entity->id()], [ 284 ':url' => $this->url('entity.menu.add_link_form', ['menu' => $this->entity->id()], [
272 'query' => ['destination' => $this->entity->url('edit-form')], 285 'query' => ['destination' => $this->entity->toUrl('edit-form')->toString()],
273 ]), 286 ]),
274 ]); 287 ]);
275 $links = $this->buildOverviewTreeForm($tree, $delta); 288 $links = $this->buildOverviewTreeForm($tree, $delta);
289
290 // Get the menu links which have pending revisions, and disable the
291 // tabledrag if there are any.
292 $edited_ids = array_filter(array_map(function ($element) {
293 return is_array($element) && isset($element['#item']) && $element['#item']->link instanceof MenuLinkContent ? $element['#item']->link->getMetaData()['entity_id'] : NULL;
294 }, $links));
295 $pending_menu_link_ids = array_intersect($this->menuLinkContentStorage->getMenuLinkIdsWithPendingRevisions(), $edited_ids);
296 if ($pending_menu_link_ids) {
297 $form['help'] = [
298 '#type' => 'container',
299 'message' => [
300 '#markup' => $this->formatPlural(
301 count($pending_menu_link_ids),
302 '%capital_name contains 1 menu link with pending revisions. Manipulation of a menu tree having links with pending revisions is not supported, but you can re-enable manipulation by getting each menu link to a published state.',
303 '%capital_name contains @count menu links with pending revisions. Manipulation of a menu tree having links with pending revisions is not supported, but you can re-enable manipulation by getting each menu link to a published state.',
304 [
305 '%capital_name' => $this->entity->label(),
306 ]
307 ),
308 ],
309 '#attributes' => ['class' => ['messages', 'messages--warning']],
310 '#weight' => -10,
311 ];
312
313 unset($form['links']['#tabledrag']);
314 unset($form['links']['#header'][2]);
315 }
316
276 foreach (Element::children($links) as $id) { 317 foreach (Element::children($links) as $id) {
277 if (isset($links[$id]['#item'])) { 318 if (isset($links[$id]['#item'])) {
278 $element = $links[$id]; 319 $element = $links[$id];
279 320
321 $is_pending_menu_link = isset($element['#item']->link->getMetaData()['entity_id'])
322 && in_array($element['#item']->link->getMetaData()['entity_id'], $pending_menu_link_ids);
323
280 $form['links'][$id]['#item'] = $element['#item']; 324 $form['links'][$id]['#item'] = $element['#item'];
281 325
282 // TableDrag: Mark the table row as draggable. 326 // TableDrag: Mark the table row as draggable.
283 $form['links'][$id]['#attributes'] = $element['#attributes']; 327 $form['links'][$id]['#attributes'] = $element['#attributes'];
284 $form['links'][$id]['#attributes']['class'][] = 'draggable'; 328 $form['links'][$id]['#attributes']['class'][] = 'draggable';
329
330 if ($is_pending_menu_link) {
331 $form['links'][$id]['#attributes']['class'][] = 'color-warning';
332 $form['links'][$id]['#attributes']['class'][] = 'menu-link-content--pending-revision';
333 }
285 334
286 // TableDrag: Sort the table row according to its existing/configured weight. 335 // TableDrag: Sort the table row according to its existing/configured weight.
287 $form['links'][$id]['#weight'] = $element['#item']->link->getWeight(); 336 $form['links'][$id]['#weight'] = $element['#item']->link->getWeight();
288 337
289 // Add special classes to be used for tabledrag.js. 338 // Add special classes to be used for tabledrag.js.
299 $element['title'], 348 $element['title'],
300 ]; 349 ];
301 $form['links'][$id]['enabled'] = $element['enabled']; 350 $form['links'][$id]['enabled'] = $element['enabled'];
302 $form['links'][$id]['enabled']['#wrapper_attributes']['class'] = ['checkbox', 'menu-enabled']; 351 $form['links'][$id]['enabled']['#wrapper_attributes']['class'] = ['checkbox', 'menu-enabled'];
303 352
304 $form['links'][$id]['weight'] = $element['weight']; 353 // Disallow changing the publishing status of a pending revision.
354 if ($is_pending_menu_link) {
355 $form['links'][$id]['enabled']['#access'] = FALSE;
356 }
357
358 if (!$pending_menu_link_ids) {
359 $form['links'][$id]['weight'] = $element['weight'];
360 }
305 361
306 // Operations (dropbutton) column. 362 // Operations (dropbutton) column.
307 $form['links'][$id]['operations'] = $element['operations']; 363 $form['links'][$id]['operations'] = $element['operations'];
308 364
309 $form['links'][$id]['id'] = $element['id']; 365 $form['links'][$id]['id'] = $element['id'];
461 if (isset($form_links[$id]['#item'])) { 517 if (isset($form_links[$id]['#item'])) {
462 $element = $form_links[$id]; 518 $element = $form_links[$id];
463 $updated_values = []; 519 $updated_values = [];
464 // Update any fields that have changed in this menu item. 520 // Update any fields that have changed in this menu item.
465 foreach ($fields as $field) { 521 foreach ($fields as $field) {
466 if ($element[$field]['#value'] != $element[$field]['#default_value']) { 522 if (isset($element[$field]['#value']) && $element[$field]['#value'] != $element[$field]['#default_value']) {
467 $updated_values[$field] = $element[$field]['#value']; 523 $updated_values[$field] = $element[$field]['#value'];
468 } 524 }
469 } 525 }
470 if ($updated_values) { 526 if ($updated_values) {
471 // Use the ID from the actual plugin instance since the hidden value 527 // Use the ID from the actual plugin instance since the hidden value