Commit ecaa5279 authored by catch's avatar catch

Issue #1938918 by joelpittet, lauriii, botanic_spark, mdrummond, nielsonm:...

Issue #1938918 by joelpittet, lauriii, botanic_spark, mdrummond, nielsonm: Convert menu theme tables to table #type.
parent bee3e48d
...@@ -2139,8 +2139,8 @@ function _drupal_add_library($library_name, $every_page = NULL) { ...@@ -2139,8 +2139,8 @@ function _drupal_add_library($library_name, $every_page = NULL) {
* In a situation where tree relationships are present, adding multiple * In a situation where tree relationships are present, adding multiple
* subgroups is not necessary, because the table will contain indentations that * subgroups is not necessary, because the table will contain indentations that
* provide enough information about the sibling and parent relationships. See * provide enough information about the sibling and parent relationships. See
* theme_menu_overview_form() for an example creating a table containing parent * MenuForm::BuildOverviewForm for an example creating a table
* relationships. * containing parent relationships.
* *
* @param $element * @param $element
* A form element to attach the tableDrag behavior to. * A form element to attach the tableDrag behavior to.
...@@ -2177,7 +2177,7 @@ function _drupal_add_library($library_name, $every_page = NULL) { ...@@ -2177,7 +2177,7 @@ function _drupal_add_library($library_name, $every_page = NULL) {
* to FALSE if the column should not be hidden. * to FALSE if the column should not be hidden.
* - 'limit': (optional) Limit the maximum amount of parenting in this table. * - 'limit': (optional) Limit the maximum amount of parenting in this table.
* *
* @see theme_menu_overview_form() * @see MenuForm::BuildOverviewForm()
*/ */
function drupal_attach_tabledrag(&$element, array $options) { function drupal_attach_tabledrag(&$element, array $options) {
// Add default values to elements. // Add default values to elements.
......
<?php
/**
* @file
* Administrative page callbacks for Menu UI module.
*/
use Drupal\Component\Utility\SafeMarkup;
use Drupal\Core\Render\Element;
/**
* Returns HTML for the menu overview form into a table.
*
* @param array $variables
* An associative array containing:
* - form: A render element representing the form.
*
* @return string
* The themed HTML.
*
* @ingroup themeable
*/
function theme_menu_overview_form($variables) {
$form = $variables['form'];
$header = array(
t('Menu link'),
array('data' => t('Enabled'), 'class' => array('checkbox')),
t('Weight'),
t('Operations'),
);
$rows = array();
foreach (Element::children($form) as $id) {
if (isset($form[$id]['#item'])) {
$element = &$form[$id];
// Add special classes to be used for tabledrag.js.
$element['parent']['#attributes']['class'] = array('menu-parent');
$element['id']['#attributes']['class'] = array('menu-id');
$element['weight']['#attributes']['class'] = array('menu-weight');
// Change the parent field to a hidden. This allows any value but hides
// the field.
$element['parent']['#type'] = 'hidden';
$indent = array(
'#theme' => 'indentation',
'#size' => $element['#item']->depth - 1,
);
$row = array();
$row[] = SafeMarkup::set(drupal_render($indent) . drupal_render($element['title']));
$row[] = array('data' => drupal_render($element['enabled']), 'class' => array('checkbox', 'menu-enabled'));
$row[] = SafeMarkup::set(drupal_render($element['weight']) . drupal_render($element['parent']) . drupal_render($element['id']));
$row[] = drupal_render($element['operations']);
$row = array_merge(array('data' => $row), $element['#attributes']);
$row['class'][] = 'draggable';
$rows[] = $row;
}
}
$output = '';
if (empty($rows)) {
$rows[] = array(array('data' => $form['#empty_text'], 'colspan' => '7'));
}
$table = array(
'#type' => 'table',
'#header' => $header,
'#rows' => $rows,
'#attributes' => array(
'id' => 'menu-overview',
),
'#tabledrag' => array(
array(
'action' => 'match',
'relationship' => 'parent',
'group' => 'menu-parent',
'subgroup' => 'menu-parent',
'source' => 'menu-id',
'hidden' => TRUE,
'limit' => \Drupal::menuTree()->maxDepth() - 1,
),
array(
'action' => 'order',
'relationship' => 'sibling',
'group' => 'menu-weight',
),
),
);
$output .= drupal_render($form['inline_actions']);
$output .= drupal_render($table);
$output .= drupal_render_children($form);
return $output;
}
...@@ -69,20 +69,6 @@ function menu_ui_entity_type_build(array &$entity_types) { ...@@ -69,20 +69,6 @@ function menu_ui_entity_type_build(array &$entity_types) {
->setLinkTemplate('add-link-form', 'entity.menu.add_link_form'); ->setLinkTemplate('add-link-form', 'entity.menu.add_link_form');
} }
/**
* Implements hook_theme().
*/
function menu_ui_theme() {
return array(
'menu_overview_form' => array(
'file' => 'menu_ui.admin.inc',
'render element' => 'form',
'function' => 'theme_menu_overview_form',
),
);
}
/** /**
* Implements hook_ENTITY_TYPE_insert( for menu entities. * Implements hook_ENTITY_TYPE_insert( for menu entities.
*/ */
......
...@@ -213,9 +213,6 @@ public function save(array $form, FormStateInterface $form_state) { ...@@ -213,9 +213,6 @@ public function save(array $form, FormStateInterface $form_state) {
protected function buildOverviewForm(array &$form, FormStateInterface $form_state) { protected function buildOverviewForm(array &$form, FormStateInterface $form_state) {
// Ensure that menu_overview_form_submit() knows the parents of this form // Ensure that menu_overview_form_submit() knows the parents of this form
// section. // section.
$form['#tree'] = TRUE;
$form['#theme'] = 'menu_overview_form';
if (!$form_state->has('menu_overview_form_parents')) { if (!$form_state->has('menu_overview_form_parents')) {
$form_state->set('menu_overview_form_parents', []); $form_state->set('menu_overview_form_parents', []);
} }
...@@ -242,10 +239,85 @@ protected function buildOverviewForm(array &$form, FormStateInterface $form_stat ...@@ -242,10 +239,85 @@ protected function buildOverviewForm(array &$form, FormStateInterface $form_stat
}; };
$delta = max($count($tree), 50); $delta = max($count($tree), 50);
$form = array_merge($form, $this->buildOverviewTreeForm($tree, $delta)); $form['links'] = array(
'#type' => 'table',
'#theme' => 'table__menu_overview',
'#header' => array(
$this->t('Menu link'),
array(
'data' => $this->t('Enabled'),
'class' => array('checkbox'),
),
$this->t('Weight'),
array(
'data' => $this->t('Operations'),
'colspan' => 3,
),
),
'#attributes' => array(
'id' => 'menu-overview',
),
'#tabledrag' => array(
array(
'action' => 'match',
'relationship' => 'parent',
'group' => 'menu-parent',
'subgroup' => 'menu-parent',
'source' => 'menu-id',
'hidden' => TRUE,
'limit' => \Drupal::menuTree()->maxDepth() - 1,
),
array(
'action' => 'order',
'relationship' => 'sibling',
'group' => 'menu-weight',
),
),
);
$destination = $this->getUrlGenerator()->getPathFromRoute('entity.menu.edit_form', array('menu' => $this->entity->id())); $destination = $this->getUrlGenerator()->getPathFromRoute('entity.menu.edit_form', array('menu' => $this->entity->id()));
$url = $destination = $this->url('entity.menu.add_link_form', array('menu' => $this->entity->id()), array('query' => array('destination' => $destination))); $url = $destination = $this->url('entity.menu.add_link_form', array('menu' => $this->entity->id()), array('query' => array('destination' => $destination)));
$form['#empty_text'] = $this->t('There are no menu links yet. <a href="@url">Add link</a>.', array('@url' => $url)); $form['links']['#empty'] = $this->t('There are no menu links yet. <a href="@url">Add link</a>.', array('@url' => $url));
$links = $this->buildOverviewTreeForm($tree, $delta);
foreach (Element::children($links) as $id) {
if (isset($links[$id]['#item'])) {
$element = $links[$id];
$form['links'][$id]['#item'] = $element['#item'];
// TableDrag: Mark the table row as draggable.
$form['links'][$id]['#attributes'] = $element['#attributes'];
$form['links'][$id]['#attributes']['class'][] = 'draggable';
$form['links'][$id]['#item'] = $element['#item'];
// TableDrag: Sort the table row according to its existing/configured weight.
$form['links'][$id]['#weight'] = $element['#item']->link->getWeight();
// Add special classes to be used for tabledrag.js.
$element['parent']['#attributes']['class'] = array('menu-parent');
$element['weight']['#attributes']['class'] = array('menu-weight');
$element['id']['#attributes']['class'] = array('menu-id');
$form['links'][$id]['title'] = array(
array(
'#theme' => 'indentation',
'#size' => $element['#item']->depth - 1,
),
$element['title'],
);
$form['links'][$id]['enabled'] = $element['enabled'];
$form['links'][$id]['enabled']['#wrapper_attributes']['class'] = array('checkbox', 'menu-enabled');
$form['links'][$id]['weight'] = $element['weight'];
// Operations (dropbutton) column.
$form['links'][$id]['operations'] = $element['operations'];
$form['links'][$id]['id'] = $element['id'];
$form['links'][$id]['parent'] = $element['parent'];
}
}
return $form; return $form;
} }
...@@ -374,9 +446,10 @@ protected function submitOverviewForm(array $complete_form, FormStateInterface $ ...@@ -374,9 +446,10 @@ protected function submitOverviewForm(array $complete_form, FormStateInterface $
$form = array_intersect_key(array_merge($order, $form), $form); $form = array_intersect_key(array_merge($order, $form), $form);
$fields = array('weight', 'parent', 'enabled'); $fields = array('weight', 'parent', 'enabled');
foreach (Element::children($form) as $id) { $form_links = $form['links'];
if (isset($form[$id]['#item'])) { foreach (Element::children($form_links) as $id) {
$element = $form[$id]; if (isset($form_links[$id]['#item'])) {
$element = $form_links[$id];
$updated_values = array(); $updated_values = array();
// Update any fields that have changed in this menu item. // Update any fields that have changed in this menu item.
foreach ($fields as $field) { foreach ($fields as $field) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment