1 tabledrag_example_parent_form.inc theme_tabledrag_example_parent_form($variables)

Theme callback for the tabledrag_example_parent_form form.

The theme callback will format the $form data structure into a table and add our tabledrag functionality. (Note that backdrop_add_tabledrag should be called from the theme layer, and not from a form declaration. This helps keep template files clean and readable, and prevents tabledrag.js from being added twice accidently.

Related topics

File

modules/examples/tabledrag_example/tabledrag_example_parent_form.inc, line 130
Example demonstrating a parent/child tabledrag form

Code

function theme_tabledrag_example_parent_form($variables) {
  $form = $variables['form'];

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

  // Iterate over each element in our $form['example_items'] array.
  foreach (element_children($form['example_items']) as $id) {

    // 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
    // backdrop_add_tabledrag call.
    //
    // This could also have been done during the form declaration by adding
    // @code
    //   '#attributes' => array('class' => 'example-item-weight'),
    // @endcode
    // directly to the 'weight' element in tabledrag_example_simple_form().
    $form['example_items'][$id]['weight']['#attributes']['class'] = array('example-item-weight');

    // In the parent/child example, we must also set this same custom class on
    // our id and parent_id columns (which could also have been done within
    // the form declaration, as above).
    $form['example_items'][$id]['id']['#attributes']['class'] = array('example-item-id');
    $form['example_items'][$id]['pid']['#attributes']['class'] = array('example-item-pid');

    // To support the tabledrag behaviour, we need to assign each row of the
    // table a class attribute of 'draggable'. This will add the 'draggable'
    // class to the <tr> element for that row when the final table is
    // rendered.
    $class = array('draggable');

    // We can add the 'tabledrag-root' class to a row in order to indicate
    // that the row may not be nested under a parent row.  In our sample data
    // for this example, the description for the item with id '8' flags it as
    // a 'root' item which should not be nested.
    if ($id == '8') {
      $class[] = 'tabledrag-root';
    }

    // We can add the 'tabledrag-leaf' class to a row in order to indicate
    // that the row may not contain child rows.  In our sample data for this
    // example, the description for the item with id '9' flags it as a 'leaf'
    // item which can not contain child items.
    if ($id == '9') {
      $class[] = 'tabledrag-leaf';
    }

    // If this is a child element, we need to add some indentation to the row,
    // so that it appears nested under its parent.  Our $depth parameter was
    // calculated while building the tree in tabledrag_example_parent_get_data
    $indent = theme('indentation', array('size' => $form['example_items'][$id]['depth']['#value']));
    unset($form['example_items'][$id]['depth']);

    // 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 backdrop_render()
    // function to generate the final html markup for that element.
    $rows[] = array(
      'data' => array(
        // Add our 'name' column, being sure to include our indentation.
        $indent . backdrop_render($form['example_items'][$id]['name']),
        // Add our 'description' column.
        backdrop_render($form['example_items'][$id]['description']),
        // Add our 'weight' column.
        backdrop_render($form['example_items'][$id]['weight']),
        // Add our hidden 'id' column.
        backdrop_render($form['example_items'][$id]['id']),
        // Add our hidden 'parent id' column.
        backdrop_render($form['example_items'][$id]['pid']),
      ),
      // To support the tabledrag behaviour, we need to assign each row of the
      // table a class attribute of 'draggable'. This will add the 'draggable'
      // class to the <tr> element for that row when the final table is
      // rendered.
      'class' => $class,
    );
  }

  // We now define the table header values.  Ensure that the 'header' count
  // matches the final column count for your table.
  //
  // Normally, we would hide the headers on our hidden columns, but we are
  // leaving them visible in this example.
  // $header = array(t('Name'), t('Description'), '', '', '');
  $header = array(t('Name'), t('Description'), t('Weight'), t('ID'), t('PID'));

  // We also need to pass the backdrop_add_tabledrag() function an id which will
  // be used to identify the <table> element containing our tabledrag form.
  // Because an element's 'id' should be unique on a page, make sure the value
  // you select is NOT the same as the form ID used in your form declaration.
  $table_id = 'example-items-table';

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

  // And then render any remaining form elements (such as our submit button).
  $output .= backdrop_render_children($form);

  // We now call the backdrop_add_tabledrag() function in order to add the
  // tabledrag.js goodness onto our page.
  //
  // For our parent/child tree table, we need to pass it:
  // - the $table_id of our <table> element (example-items-table),
  // - the $action to be performed on our form items ('match'),
  // - a string describing where $action should be applied ('parent'),
  // - the $group value (pid column) class name ('example-item-pid'),
  // - the $subgroup value (pid column) class name ('example-item-pid'),
  // - the $source value (id column) class name ('example-item-id'),
  // - an optional $hidden flag identifying if the columns should be hidden,
  // - an optional $limit parameter to control the max parenting depth.
  backdrop_add_tabledrag($table_id, 'match', 'parent', 'example-item-pid', 'example-item-pid', 'example-item-id', FALSE);

  // Because we also want to sort in addition to providing parenting, we call
  // the backdrop_add_tabledrag function again, instructing it to update the
  // weight field as items at the same level are re-ordered.
  backdrop_add_tabledrag($table_id, 'order', 'sibling', 'example-item-weight', NULL, NULL, FALSE);

  return $output;
}