You are here

function theme_commerce_pricelist_draggable_form in Commerce Pricelist 7

Theme callback for the commerce_pricelist_draggable_form form.

The theme callback will format the $form data structure into a table and add our tabledrag functionality. (Note that drupal_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.

Return value

array The rendered tabledrag form

File

includes/commerce_pricelist.admin.inc, line 312
Summary

Code

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

  // Initialize the variable which will store our table rows.
  $rows = array();
  if (isset($form['pricelists'])) {

    // Iterate over each element in our $form['pricelists'] array.
    foreach (element_children($form['pricelists']) 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
      // drupal_add_tabledrag call.
      //
      // This could also have been done during the form declaration by adding
      // '#attributes' => array('class' => 'pricelist-weight'),
      // directy to the 'weight' element in commerce_pricelist_draggable_form().
      $form['pricelists'][$id]['weight']['#attributes']['class'] = array(
        'pricelist-weight',
      );

      // 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(
          // Add our 'name' column.
          drupal_render($form['pricelists'][$id]['title']),
          // Add our 'description' column.
          drupal_render($form['pricelists'][$id]['status']),
          // Add our 'weight' column.
          drupal_render($form['pricelists'][$id]['weight']),
          drupal_render($form['pricelists'][$id]['data']),
          drupal_render($form['pricelists'][$id]['rows']),
          drupal_render($form['pricelists'][$id]['view']),
          drupal_render($form['pricelists'][$id]['add']),
          drupal_render($form['pricelists'][$id]['edit']),
        ),
        // 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',
        ),
      );
    }
  }
  $header = array(
    t('Title'),
    t('Status'),
    t('Weight'),
    t('Filters'),
    t('Rows'),
    array(
      'data' => t('Operations'),
      'colspan' => '3',
    ),
  );
  $table_id = 'pricelists-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 .= drupal_render_children($form);

  // We now call the drupal_add_tabledrag() function in order to add the
  // tabledrag.js goodness onto our page.
  //
  // For a basic sortable table, we need to pass it:
  // - the $table_id of our <table> element,
  // - the $action to be performed on our form items ('order'),
  // - a string describing where $action should be applied ('siblings'),
  // - and the class of the element containing our 'weight' element.
  drupal_add_tabledrag($table_id, 'order', 'sibling', 'pricelist-weight');
  return $output;
}