You are here

function theme_entity_rules_bundle_rules_form in Entity Rules 7

Theme callback for the entity_rules_bundle_rules_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

The rendered tabledrag form

File

./entity_rules.admin.inc, line 357
Admin functions.

Code

function theme_entity_rules_bundle_rules_form($variables) {
  $form = $variables['form'];
  if (empty($form['settings'])) {

    // If there are Rule settings then we don't need the table
    return drupal_render_children($form);
  }
  $have_false_msg = $have_args = FALSE;

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

  // Iterate over each element in our $form[$setting_var_name] array.
  foreach (element_children($form['settings']) as $id) {
    if (isset($form['settings'][$id]['false_msg'])) {
      $have_false_msg = TRUE;
    }
    if (isset($form['settings'][$id]['args'])) {
      $have_args = TRUE;
    }
  }
  foreach (element_children($form['settings']) 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' => 'example-item-weight'),
    // directy to the 'weight' element in tabledrag_example_simple_form().
    $form['settings'][$id]['weight']['#attributes']['class'] = array(
      'rule-item-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.
    $row = array(
      'data' => array(
        // Add our 'name' column.
        drupal_render($form['settings'][$id]['name']),
      ),
      'class' => array(
        'draggable',
      ),
    );
    if ($have_args) {
      $have_args = TRUE;

      // Add our 'description' column.
      $row['data'][] = drupal_render($form['settings'][$id]['args']);
    }
    if ($have_false_msg) {

      // Add our 'return message' column.
      $row['data'][] = drupal_render($form['settings'][$id]['false_msg']);
    }

    // Add our 'remove' column
    $row['data'][] = drupal_render($form['settings'][$id]['remove_link']);

    // Add our 'weight' column.
    $row['data'][] = drupal_render($form['settings'][$id]['weight']);
    $rows[] = $row;
  }

  // We now define the table header values.  Ensure that the 'header' count
  // matches the final column count for your table.
  $header = array(
    t('Name'),
  );
  if ($have_args) {
    $header[] = t('Arguments');
  }
  if ($have_false_msg) {
    $header[] = t('Return Message');
  }
  $header[] = t('Remove');
  $header[] = t('Weight');

  // 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.
  // 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 = '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 .= drupal_render_children($form);
  if (!empty($table_id)) {
    drupal_add_tabledrag($table_id, 'order', 'sibling', 'rule-item-weight');
  }
  return $output;
}