You are here

function theme_menu_link_weight_reorder_element in Menu Link Weight 7

Theme callback for the menu_link_weight element in the node form.

See also

theme_tabledrag_example_simple_form()

1 theme call to theme_menu_link_weight_reorder_element()
menu_link_weight_form_node_form_alter in ./menu_link_weight.module
Implements hook_form_FORM_ID_alter().

File

./menu_link_weight.module, line 477
Replaces the menu link weight dropdown with a tabledrag widget.

Code

function theme_menu_link_weight_reorder_element($variables) {
  $element = $variables['element'];

  // Initialize the variable which will store our table rows.
  $rows = array();

  // Iterate over each element in our $form['menu']['link']['menu_link_weight']
  // array.
  foreach (element_children($element) as $id) {
    $has_tabledrag = TRUE;

    // Before we add our 'weight' column to the row, we need to give the
    // element a custom class so that it can be identified in the
    // drupal_add_tabledrag call.
    $weight_class = 'menu-link-weight-item-weight';
    $element[$id]['weight']['#attributes']['class'] = array(
      $weight_class,
    );

    // To support the tabledrag behaviour, we need to assign each row of the
    // table a class attribute of 'draggable'.
    $class = array(
      'draggable',
    );
    $no_striping = FALSE;
    if ($id == 'link_current') {

      // Make the row which contains the menu link for the node we are editing
      // highlight, by using the "warning" class.
      $class[] = 'warning';
      $class[] = 'menu-link-weight-link-current-row';
      $no_striping = TRUE;
    }

    // We are now ready to add each element of our $form data to the $rows
    // array, so that they end up as individual table cells when rendered
    // in the final table.  We run each element through the drupal_render()
    // function to generate the final html markup for that element.
    $rows[] = array(
      'data' => array(
        drupal_render($element[$id]['name']),
        drupal_render($element[$id]['weight']),
      ),
      'class' => $class,
      'no_striping' => $no_striping,
    );
  }

  // We also need to pass the drupal_add_tabledrag() function an id which will
  // be used to identify the <table> element containing our tabledrag form.
  $table_id = 'menu-link-weight-reorder';

  // We can render our tabledrag table for output.
  $output = theme('table', array(
    'header' => array(
      t('Name'),
      t('Weight'),
    ),
    'rows' => $rows,
    'attributes' => array(
      'id' => $table_id,
    ),
  ));

  // We now call the drupal_add_tabledrag() function in order to add the
  // tabledrag.js functionality onto our page.
  if (isset($has_tabledrag) && $has_tabledrag) {
    drupal_add_tabledrag($table_id, 'order', 'sibling', $weight_class);
  }
  return $output;
}