Form constructor to edit an entire menu tree at once.
Shows for one menu the menu links accessible to the current user and relevant operations.
This form constructor can be integrated as a section into another form. It relies on the following keys in $form_state:
Forms integrating this section should call menu_overview_form_submit() from their form submit handler.
function menu_overview_form($form, &$form_state) {
global $menu_admin;
// Ensure that menu_overview_form_submit() knows the parents of this form
// section.
$form['#tree'] = TRUE;
$form['#theme'] = 'menu_overview_form';
$form_state += array(
'menu_overview_form_parents' => array(),
);
$form['#attached']['css'] = array(
drupal_get_path('module', 'menu') . '/css/menu.admin.css',
);
$links = array();
$query = Drupal::entityQuery('menu_link')
->condition('menu_name', $form_state['menu']
->id());
for ($i = 1; $i <= MENU_MAX_DEPTH; $i++) {
$query
->sort('p' . $i, 'ASC');
}
$result = $query
->execute();
if (!empty($result)) {
$links = menu_link_load_multiple($result);
}
$delta = max(count($links), 50);
$tree = menu_tree_data($links);
$node_links = array();
menu_tree_collect_node_links($tree, $node_links);
// We indicate that a menu administrator is running the menu access check.
$menu_admin = TRUE;
menu_tree_check_access($tree, $node_links);
$menu_admin = FALSE;
// Inline the "Add link" action so it displays right above the table of
// links. No access check needed, since this form has the same access
// restriction as adding menu items to the menu.
$form['inline_actions'] = array(
'#prefix' => '<ul class="action-links">',
'#suffix' => '</ul>',
);
$form['inline_actions']['add'] = array(
'#theme' => 'menu_local_action',
'#link' => array(
'href' => 'admin/structure/menu/manage/' . $form_state['menu']
->id() . '/add',
'title' => t('Add link'),
),
);
$form = array_merge($form, _menu_overview_tree_form($tree, $delta));
$form['#empty_text'] = t('There are no menu links yet. <a href="@link">Add link</a>.', array(
'@link' => url('admin/structure/menu/manage/' . $form_state['menu']
->id() . '/add'),
));
return $form;
}