You are here

function elements_pre_render_table in Elements 7

#pre_render callback to transform children of an element into #rows suitable for theme_table().

This function converts sub-elements of an element of #type 'table' to be suitable for theme_table():

  • The first level of sub-elements are table rows. Only the #attributes property is taken into account.
  • The second level of sub-elements is converted into columns for the corresponding first-level table row.

Simple example usage:

$form['table'] = array(
  '#type' => 'table',
  '#header' => array(
    t('Title'),
    array(
      'data' => t('Operations'),
      'colspan' => '1',
    ),
  ),
  // Optionally, to add tableDrag support:
  '#tabledrag' => array(
    array(
      'order',
      'sibling',
      'thing-weight',
    ),
  ),
);
foreach ($things as $row => $thing) {
  $form['table'][$row]['#weight'] = $thing['weight'];
  $form['table'][$row]['title'] = array(
    '#type' => 'textfield',
    '#default_value' => $thing['title'],
  );

  // Optionally, to add tableDrag support:
  $form['table'][$row]['#attributes']['class'][] = 'draggable';
  $form['table'][$row]['weight'] = array(
    '#type' => 'textfield',
    '#title' => t('Weight for @title', array(
      '@title' => $thing['title'],
    )),
    '#title_display' => 'invisible',
    '#size' => 4,
    '#default_value' => $thing['weight'],
    '#attributes' => array(
      'class' => array(
        'thing-weight',
      ),
    ),
  );

  // The amount of link columns should be identical to the 'colspan'
  // attribute in #header above.
  $form['table'][$row]['edit'] = array(
    '#type' => 'link',
    '#title' => t('Edit'),
    '#href' => 'thing/' . $row . '/edit',
  );
}

Parameters

array $element: A structured array containing two sub-levels of elements. Properties used:

  • #tabledrag: The value is a list of arrays that are passed to drupal_add_tabledrag(). The HTML ID of the table is prepended to each set of arguments.

See also

elements_element_info()

theme_table()

drupal_process_attached()

drupal_add_tabledrag()

1 string reference to 'elements_pre_render_table'
elements_element_info in ./elements.module
Implements hook_element_info().

File

./elements.module, line 557

Code

function elements_pre_render_table(array $element) {
  foreach (element_children($element) as $first) {
    $row = array(
      'data' => array(),
    );

    // Apply attributes of first-level elements as table row attributes.
    if (isset($element[$first]['#attributes'])) {
      $row += $element[$first]['#attributes'];
    }

    // Turn second-level elements into table row columns.
    // @todo Do not render a cell for children of #type 'value'.
    // @see http://drupal.org/node/1248940
    foreach (element_children($element[$first]) as $second) {

      // Assign the element by reference, so any potential changes to the
      // original element are taken over.
      $column = array(
        'data' => &$element[$first][$second],
      );

      // Apply wrapper attributes of second-level elements as table cell
      // attributes.

      //if (isset($element[$first][$second]['#wrapper_attributes'])) {

      //  $column += $element[$first][$second]['#wrapper_attributes'];

      //}
      $row['data'][] = $column;
    }
    $element['#rows'][] = $row;
  }

  // Take over $element['#id'] as HTML ID attribute, if not already set.
  element_set_attributes($element, array(
    'id',
  ));

  // If the custom #tabledrag is set and there is a HTML ID, inject the table's
  // HTML ID as first callback argument and attach the behavior.
  if (!empty($element['#tabledrag']) && isset($element['#attributes']['id'])) {
    foreach ($element['#tabledrag'] as &$args) {
      array_unshift($args, $element['#attributes']['id']);
    }
    $element['#attached']['drupal_add_tabledrag'] = $element['#tabledrag'];
  }
  if (!empty($element['#tabledrag']) && !empty($element['#tableselect'])) {
    $element['#attached']['css'][] = drupal_get_path('module', 'elements') . '/elements.table.css';
  }
  return $element;
}