You are here

cck_table.module in CCK Table Field 6

Same filename and directory in other branches
  1. 8 cck_table.module
  2. 5 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

define('CCK_TABLE_ENFORCE_MISALIGN_COL', TRUE);
define('CCK_TABLE_DEFAULT_ROWS', 5);
define('CCK_TABLE_DEFAULT_SEPARATOR', '|');

/**
 *  Module version 5 create by rszrama
 *  sponser by Wombats http://www.bywombats.com
 *  Module coverted to 6 by iStryker
 *  Sponser by Themes 24/7  http://www.themes247.com
 */

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

/**
 * Implementation of hook_theme().
 */
function cck_table_theme() {
  return array(
    'cck_table' => array(
      'arguments' => array(
        'element' => NULL,
      ),
    ),
    'cck_table_formatter_first_row_header' => array(
      'arguments' => array(
        'element' => NULL,
      ),
      'function' => 'theme_cck_table_formatter',
    ),
    'cck_table_formatter_without_header' => array(
      'arguments' => array(
        'element' => NULL,
      ),
      'function' => 'theme_cck_table_formatter',
    ),
  );
}

/**
 * Implementation of hook_field_info().
 */
function cck_table_field_info() {
  return array(
    'table' => array(
      'label' => t('Table'),
      'description' => t('Defines a textarea field that outputs data in a table'),
    ),
  );
}

/**
 * Implementation of hook_field_settings().
 */
function cck_table_field_settings($op, $field) {
  switch ($op) {
    case 'form':
      $form = array();
      $form['separator'] = array(
        '#type' => 'textfield',
        '#title' => t('Separator'),
        '#description' => t('Separator to distingush the cells in each row'),
        '#default_value' => isset($field['separator']) ? $field['separator'] : CCK_TABLE_DEFAULT_SEPARATOR,
        '#size' => 5,
        '#required' => TRUE,
      );
      $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(
        'separator',
        'css_id',
        'css_class',
      );
    case 'database columns':
      $columns['value'] = array(
        'type' => 'text',
        'not null' => FALSE,
        'default' => NULL,
        'sortable' => FALSE,
      );
      return $columns;
    case 'filters':
      break;
  }
}

/**
 * Implementation of hook_content_is_empty().
 */
function cck_table_content_is_empty($item, $field) {
  if (empty($item['value']) && (string) $item['value'] !== '0') {
    return TRUE;
  }
  return FALSE;
}

/**
 * Implementation of hook_field_formatter_info().
 */
function cck_table_field_formatter_info() {
  return array(
    'first_row_header' => array(
      'label' => t('First Row will be the table header'),
      'multiple values' => CONTENT_HANDLE_CORE,
      'field types' => array(
        'table',
      ),
    ),
    'without_header' => array(
      'label' => t('Without a table header'),
      'multiple values' => CONTENT_HANDLE_CORE,
      'field types' => array(
        'table',
      ),
    ),
  );
}

/**
 * Implementation of hook_field_formatter().
 */
function theme_cck_table_formatter($element) {
  $field = content_fields($element['#field_name'], $element['#type_name']);
  $value = $element['#item']['value'];
  switch ($element['#formatter']) {
    case 'first_row_header':
      $header_type = TRUE;
      break;
    case 'without_header':
      $header_type = FALSE;
      break;
  }
  if (!empty($value)) {
    $header = array();
    $rows = array();
    $lines = explode("\n", $value);
    $lines = array_map('trim', $lines);
    $lines = array_filter($lines, 'strlen');
    foreach ($lines as $line) {
      $cells = explode($field['separator'], $line);
      if (count($header) == 0 && count($lines) > 1 && $header_type) {
        $header = $cells;
      }
      else {
        $rows[] = $cells;
      }
    }
    if (count($rows) > 0) {
      $attributes = array();
      if (!empty($field['css_id'])) {
        $attributes['id'] = $field['css_id'] . '-' . $element['#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(
    'cck_table' => array(
      'label' => t('Textarea'),
      'field types' => array(
        'table',
      ),
      'multiple values' => CONTENT_HANDLE_CORE,
      'callbacks' => array(
        'default value' => CONTENT_CALLBACK_DEFAULT,
      ),
    ),
  );
}

/**
 * Implementation of hook_widget_settings().
 */
function cck_table_widget_settings($op, $widget) {
  switch ($op) {
    case 'form':
      $form = array();
      $rows = isset($widget['rows']) && is_numeric($widget['rows']) ? $widget['rows'] : CCK_TABLE_DEFAULT_ROWS;
      $misalign_col = isset($widget['enforce_misalign_col']) ? $widget['enforce_misalign_col'] : CCK_TABLE_ENFORCE_MISALIGN_COL;
      $form['rows'] = array(
        '#type' => 'textfield',
        '#title' => t('Rows'),
        '#default_value' => $rows,
        '#element_validate' => array(
          '_element_validate_integer_positive',
        ),
        '#required' => TRUE,
      );
      $form['enforce_misalign_col'] = array(
        '#type' => 'checkbox',
        '#title' => 'Enforce Mis-Align Column',
        '#default_value' => $misalign_col,
        '#description' => 'If Enabled, each row must have the same number of columns or an error will be thrown.',
      );
      return $form;
    case 'save':
      return array(
        'rows',
        'enforce_misalign_col',
      );
  }
}

/**
 * Implementation of FAPI hook_elements().
 *
 * Any FAPI callbacks needed for individual widgets can be declared here,
 * and the element will be passed to those callbacks for processing.
 *
 * Drupal will automatically theme the element using a theme with
 * the same name as the hook_elements key.
 *
 * Autocomplete_path is not used by text_widget but other widgets can use it
 * (see nodereference and userreference).
 */
function cck_table_elements() {
  return array(
    'cck_table' => array(
      '#input' => TRUE,
      '#columns' => array(
        'value',
      ),
      '#delta' => 0,
      '#process' => array(
        'cck_table_process',
      ),
    ),
  );
}

/**
 * Implementation of hook_field().
 */
function cck_table_field($op, &$node, $field, &$items, $teaser, $page) {
  switch ($op) {
    case 'validate':
      if ($field['widget']['enforce_misalign_col']) {
        $allowed_values = content_allowed_values($field);
        if (is_array($items)) {
          foreach ($items as $delta => $item) {
            $error_element = isset($item['_error_element']) ? $item['_error_element'] : '';
            if (is_array($item) && isset($item['_error_element'])) {
              unset($item['_error_element']);
            }
            if ($item['value'] != '') {
              $lines = explode("\n", $item['value']);
              $lines = array_map('trim', $lines);
              $lines = array_filter($lines, 'strlen');
              $cell_count;
              foreach ($lines as $line) {
                if (!$cell_count) {
                  $cell_count = count(explode('|', $line));
                }
                elseif ($cell_count != count(explode('|', $line))) {
                  form_set_error($error_element, t('%name: mis-align number of columns.', array(
                    '%name' => t($field['widget']['label']),
                  )));
                  break;
                }
              }
            }
          }
        }
      }
      return $items;
  }
}

/**
 * Implementation of hook_widget().
 *
 * Attach a single form element to the form. It will be built out and
 * validated in the callback(s) listed in hook_elements. We build it
 * out in the callbacks rather than here in hook_widget so it can be
 * plugged into any module that can provide it with valid
 * $field information.
 *
 * Content module will set the weight, field name and delta values
 * for each form element. This is a change from earlier CCK versions
 * where the widget managed its own multiple values.
 *
 * If there are multiple values for this field, the content module will
 * call this function as many times as needed.
 *
 * @param $form
 *   the entire form array, $form['#node'] holds node information
 * @param $form_state
 *   the form_state, $form_state['values'][$field['field_name']]
 *   holds the field's form values.
 * @param $field
 *   the field array
 * @param $items
 *   array of default values for this field
 * @param $delta
 *   the order of this item in the array of subelements (0, 1, 2, etc)
 *
 * @return
 *   the form item for a single element for this field
 */
function cck_table_widget(&$form, &$form_state, $field, $items, $delta = 0) {
  $element = array(
    '#type' => $field['widget']['type'],
    '#default_value' => isset($items[$delta]) ? $items[$delta] : '',
  );
  return $element;
}
function cck_table_process($element, $edit, $form_state, $form) {
  $field = $form['#field_info'][$element['#field_name']];
  $field_key = $element['#columns'][0];
  $element[$field_key] = array(
    '#type' => 'textarea',
    '#default_value' => isset($element['#value'][$field_key]) ? $element['#value'][$field_key] : NULL,
    '#rows' => !empty($field['widget']['rows']) ? $field['widget']['rows'] : CCK_TABLE_DEFAULT_ROWS,
    '#weight' => 0,
    // The following values were set by the content module and need
    // to be passed down to the nested element.
    '#title' => $element['#title'],
    '#required' => $element['#required'],
    '#field_name' => $element['#field_name'],
    '#type_name' => $element['#type_name'],
    '#delta' => $element['#delta'],
    '#columns' => $element['#columns'],
  );

  //if help text set then display help text, if not display custom help text.
  if (!empty($element['#description'])) {
    $element[$field_key]['#description'] = $element['#description'];
  }
  else {
    $element[$field_key]['#description'] = t('Enter table cell data separated by |, one row per line. By default the table header information with be the first row, (see ') . "<a href='" . base_path() . "admin/content/node-type/" . $form['type']['#value'] . "/display'>" . t('Display Field') . "</a> " . t('settings)');
  }

  // Used so that hook_field('validate') knows where to flag an error.
  $element['_error_element'] = array(
    '#type' => 'value',
    '#value' => implode('][', array_merge($element['#parents'], array(
      $field_key,
    ))),
  );
  return $element;
}

/**
 * FAPI theme for an individual text elements.
 *
 * The textfield or textarea is already rendered by the
 * textfield or textarea themes and the html output
 * lives in $element['#children']. Override this theme to
 * make custom changes to the output.
 *
 * $element['#field_name'] contains the field name
 * $element['#delta]  is the position of this element in the group
 */
function theme_cck_table($element) {
  return $element['#children'];
}

Functions

Namesort descending Description
cck_table_content_is_empty Implementation of hook_content_is_empty().
cck_table_elements Implementation of FAPI hook_elements().
cck_table_field Implementation of hook_field().
cck_table_field_formatter_info Implementation of hook_field_formatter_info().
cck_table_field_info Implementation of hook_field_info().
cck_table_field_settings Implementation of hook_field_settings().
cck_table_process
cck_table_theme Implementation of hook_theme().
cck_table_widget Implementation of hook_widget().
cck_table_widget_info Implementation of hook_widget_info().
cck_table_widget_settings Implementation of hook_widget_settings().
theme_cck_table FAPI theme for an individual text elements.
theme_cck_table_formatter Implementation of hook_field_formatter().

Constants