You are here

function form_process_table in Clock 7.2

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

Parameters

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

array $form_state: The current state of the form.

Return value

array The processed element.

See also

form_process_tableselect()

theme_tableselect()

1 string reference to 'form_process_table'
clock_element_info in ./clock.module
Implements hook_element_info().

File

./clock.table.inc, line 60
Backports the 'table' form element from Drupal 7

Code

function form_process_table($element, &$form_state) {
  if ($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.
    // @todo D8: Rename into #select_all?
    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 (!isset($element['#default_value']) || $element['#default_value'] === 0) {
      $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.
        // @todo Consider to add an optional $element[$key]['#title_key']
        //   defaulting to 'title'?
        $title = '';
        if (!empty($element[$key]['title']['#title'])) {
          $title = $element[$key]['title']['#title'];
        }
        else {
          foreach (element_children($element[$key]) as $column) {
            if (isset($element[$key][$column]['#title'])) {
              $title = $element[$key][$column]['#title'];
              break;
            }
            if (isset($element[$key][$column]['#markup'])) {
              $title = $element[$key][$column]['#markup'];
              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;
}