You are here

cck_table.module in CCK Table Field 5

Same filename and directory in other branches
  1. 8 cck_table.module
  2. 6 cck_table.module
  3. 7 cck_table.module

Defines a field type that outputs data in a table.

File

cck_table.module
View source
<?php

/**
 * @file
 * Defines a field type that outputs data in a table.
 */

/**
 * Implementation of hook_field_info().
 */
function cck_table_field_info() {
  return array(
    'tablefield' => array(
      'label' => t('Table'),
    ),
  );
}

/**
 * Implementation of hook_field_settings().
 */
function cck_table_field_settings($op, $field) {
  switch ($op) {
    case 'form':
      $form = array();
      $form['css_id'] = array(
        '#type' => 'textfield',
        '#title' => t('CSS ID'),
        '#description' => t('Specify an ID to be assigned to the table element.') . '<br />' . t('The node ID will be appended to keep the ID unique.'),
        '#default_value' => isset($field['css_id']) ? $field['css_id'] : '',
        '#field_suffix' => '-##',
      );
      $form['css_class'] = array(
        '#type' => 'textfield',
        '#title' => t('CSS Class'),
        '#description' => t('Specify a class to be added to the table element.'),
        '#default_value' => isset($field['css_class']) ? $field['css_class'] : '',
      );
      return $form;
    case 'validate':
      break;
    case 'save':
      return array(
        'css_id',
        'css_class',
      );
    case 'database columns':
      if ($field['type'] == 'tablefield') {
        return array(
          'value' => array(
            'type' => 'text',
            'not null' => FALSE,
            'default' => NULL,
            'sortable' => FALSE,
          ),
        );
      }
    case 'filters':
      break;
  }
}

/**
 * Implementation of hook_field_formatter_info().
 */
function cck_table_field_formatter_info() {
  return array(
    'default' => array(
      'label' => t('Default'),
      'field types' => array(
        'tablefield',
      ),
    ),
  );
}

/**
 * Implementation of hook_field_formatter().
 */
function cck_table_field_formatter($field, $item, $formatter, $node) {
  if (!isset($item['value'])) {
    return '';
  }
  switch ($formatter) {
    case 'default':
      $data = $item['value'];
  }
  if (!empty($data)) {
    $header = array();
    $rows = array();
    $lines = explode("\n", $data);
    $lines = array_map('trim', $lines);
    $lines = array_filter($lines, 'strlen');
    foreach ($lines as $line) {
      $cells = explode('|', $line);
      if (count($header) == 0 && count($lines) > 1) {
        $header = $cells;
      }
      else {
        $rows[] = $cells;
      }
    }
    if (count($rows) > 0) {
      $attributes = array();
      if (!empty($field['css_id'])) {
        $attributes['id'] = $field['css_id'] . '-' . $node->nid;
      }
      if (!empty($field['css_class'])) {
        $attributes['class'] = $field['css_class'];
      }
      return theme('table', $header, $rows, $attributes);
    }
  }
}

/**
 * Implementation of hook_widget_info().
 */
function cck_table_widget_info() {
  return array(
    'table_textarea' => array(
      'label' => t('Textarea'),
      'field types' => array(
        'tablefield',
      ),
    ),
  );
}

/**
 * Implementation of hook_widget_settings().
 */
function cck_table_widget_settings($op, $widget) {
  switch ($op) {
    case 'form':
      $form = array();
      $form['rows'] = array(
        '#type' => 'textfield',
        '#title' => t('Rows'),
        '#default_value' => $widget['rows'] ? $widget['rows'] : 5,
        '#required' => TRUE,
      );
      return $form;
    case 'validate':
      if (!is_numeric($widget['rows']) || intval($widget['rows']) != $widget['rows'] || $widget['rows'] <= 1) {
        form_set_error('rows', t('"Rows" must be a positive integer greater than 1.'));
      }
      break;
    case 'save':
      return array(
        'rows',
      );
  }
}

/**
 * Implementation of hook_widget().
 */
function cck_table_widget($op, &$node, $field, &$items) {
  switch ($op) {
    case 'form':
      $form = array();
      $form[$field['field_name']] = array(
        '#tree' => TRUE,
      );
      $form[$field['field_name']][0]['value'] = array(
        '#type' => 'textarea',
        '#title' => t($field['widget']['label']),
        '#default_value' => $items[0]['value'],
        '#required' => $field['required'],
        '#rows' => $field['widget']['rows'],
        '#description' => t('Enter table cell data separated by |, one row per line. The first line contains the table header information.') . '<br />' . t($field['widget']['description']),
        '#weight' => $field['widget']['weight'],
      );
      return $form;
    case 'process form values':

      // Don't save empty fields except the first value
      foreach ($items as $delta => $item) {
        if ($item['value'] == '' && $delta > 0) {
          unset($items[$delta]);
        }
      }
      break;
  }
}