You are here

function table_element_process_table in Table Element 7

A #process callback for #type 'table' to add tableselect support.

Parameters

array $element: An associative array containing the properties and children of the table element.

Return value

array The processed element.

See also

form_process_tableselect()

theme_tableselect()

1 string reference to 'table_element_process_table'
table_element_element_info in ./table_element.module
Implements hook_element_info().

File

./table_element.module, line 82
Provides a table element for Drupal 7.

Code

function table_element_process_table(array $element) {
  if (!empty($element['#tableselect'])) {
    if ($element['#multiple']) {
      $value = is_array($element['#value']) ? $element['#value'] : array();
    }
    else {
      $element['#js_select'] = FALSE;
    }

    // Add a "Select all" checkbox column to the header.
    if ($element['#js_select']) {
      $element['#attached']['library'][] = array(
        'system',
        'drupal.tableselect',
      );
      array_unshift($element['#header'], array(
        'class' => array(
          'select-all',
        ),
      ));
    }
    else {
      array_unshift($element['#header'], '');
    }
    if (empty($element['#default_value'])) {
      $element['#default_value'] = array();
    }

    // Create a checkbox or radio for each row in a way that the value of the
    // tableselect element behaves as if it had been of #type checkboxes or
    // radios.
    foreach (element_children($element) as $key) {

      // Do not overwrite manually created children.
      if (!isset($element[$key]['select'])) {

        // Determine option label; either an assumed 'title' column, or the
        // first available column containing a #title or #markup.
        $title = '';
        if (!empty($element[$key]['title']['#title'])) {
          $title = $element[$key]['title']['#title'];
        }
        else {
          foreach (element_children($element[$key]) as $column) {
            foreach (array(
              '#title',
              '#markup',
            ) as $property) {
              if (isset($element[$key][$column][$property])) {
                $title = $element[$key][$column][$property];
                break;
              }
            }
          }
        }
        if ('' !== $title) {
          $title = t('Update !title', array(
            '!title' => $title,
          ));
        }

        // Prepend the select column to existing columns.
        $element[$key] = array(
          'select' => array(),
        ) + $element[$key];
        $element[$key]['select'] += array(
          '#type' => $element['#multiple'] ? 'checkbox' : 'radio',
          '#title' => $title,
          '#title_display' => 'invisible',
          // @todo If rows happen to use numeric indexes instead of string keys,
          // this results in a first row with $key === 0, which is always FALSE.
          '#return_value' => $key,
          '#attributes' => $element['#attributes'],
        );
        $element_parents = array_merge($element['#parents'], array(
          $key,
        ));
        if ($element['#multiple']) {
          $element[$key]['select']['#default_value'] = isset($value[$key]) ? $key : NULL;
          $element[$key]['select']['#parents'] = $element_parents;
        }
        else {
          $element[$key]['select']['#default_value'] = $element['#default_value'] == $key ? $key : NULL;
          $element[$key]['select']['#parents'] = $element['#parents'];
          $element[$key]['select']['#id'] = drupal_html_id('edit-' . implode('-', $element_parents));
        }
      }
    }
  }
  return $element;
}