View source
<?php
define('CCK_TABLE_DEFAULT_ROWS', 5);
function cck_table_field_info() {
return array(
'table' => array(
'label' => t('Table'),
'description' => t('Defines a textarea field that outputs data in a table'),
'settings' => array(
'rows' => CCK_TABLE_DEFAULT_ROWS,
),
'default_widget' => 'text_textarea',
'default_formatter' => 'without_header',
),
);
}
function cck_table_field_widget_info() {
return array(
'cck_table_textarea' => array(
'label' => t('Textarea'),
'field types' => array(
'table',
),
'behaviors' => array(
'multiple values' => FIELD_BEHAVIOR_DEFAULT,
'default value' => FIELD_BEHAVIOR_NONE,
),
'settings' => array(
'rows' => CCK_TABLE_DEFAULT_ROWS,
),
),
);
}
function cck_table_field_formatter_info() {
return array(
'first_row_header' => array(
'label' => t('First Row will be the table header'),
'multiple values' => FIELD_BEHAVIOR_DEFAULT,
'field types' => array(
'table',
),
),
'without_header' => array(
'label' => t('Without a table header'),
'multiple values' => FIELD_BEHAVIOR_DEFAULT,
'field types' => array(
'table',
),
),
);
}
function cck_table_field_is_empty($item, $field) {
if (empty($item['table'])) {
return TRUE;
}
return FALSE;
}
function cck_table_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$element = array();
$settings = $display['settings'];
$formatter = $display['type'];
switch ($formatter) {
case 'first_row_header':
$header_type = TRUE;
break;
case 'without_header':
$header_type = FALSE;
break;
}
foreach ($items as $delta => $item) {
if (!empty($item['table'])) {
$header = array();
$rows = array();
$lines = explode("\n", $item['table']);
$lines = array_map('trim', $lines);
$lines = array_filter($lines, 'strlen');
foreach ($lines as $line) {
$cells = explode('|', $line);
if (empty($header) && count($lines) > 1 && $header_type) {
$header = $cells;
}
else {
$rows[] = $cells;
}
}
if (count($rows) > 0) {
$attributes = array();
if (!empty($field['css_id'])) {
$attributes['id'] = $instance['widget']['settings']['css_id'] . '-' . $instanc['id'];
}
if (!empty($field['css_class'])) {
$attributes['class'] = $instance['widget']['settings']['css_class'];
}
}
$element[$delta] = array(
'#markup' => theme('table', array(
'header' => $header,
'rows' => $rows,
'attributes' => $attributes,
)),
);
}
}
return $element;
}
function cck_table_field_widget_settings_form($field, $instance) {
$form = array();
$form['rows'] = array(
'#type' => 'textfield',
'#title' => t('Number of Rows'),
'#default_value' => isset($instance['widget']['settings']['rows']) ? $instance['widget']['settings']['rows'] : CCK_TABLE_DEFAULT_ROWS,
'#required' => TRUE,
'#element_validate' => array(
'_element_validate_integer_positive',
),
);
$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($instance['widget']['settings']['css_id']) ? $instance['widget']['settings']['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($instance['widget']['settings']['css_class']) ? $instance['widget']['settings']['css_class'] : '',
);
return $form;
}
function cck_table_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $base) {
if (!empty($instance['description'])) {
$description = t($instance['description']);
}
else {
$description = t(_cck_table_default_help_text($instance['display']['default']['type']));
}
$element = $base;
$element['table'] = array(
'#type' => 'textarea',
'#default_value' => isset($items[$delta]['table']) ? $items[$delta]['table'] : NULL,
'#rows' => $instance['widget']['settings']['rows'],
'#description' => $description,
);
return $element;
}
function _cck_table_default_help_text($display) {
switch ($display) {
case 'first_row_header':
$current_display = "Currently the first row is the header to this table";
break;
case 'without_header':
$current_display = "Currently there is no header to this table";
break;
}
$default_description = "Enter table cell data separated by |, one row per line. {$current_display}";
return $default_description;
}
function cck_table_field_widget_error($element, $error, $form, &$form_state) {
form_error($element['value'], $error['message']);
}
function cck_table_content_migrate_instance_alter(&$instance_value, &$field_value) {
if ($field_value['module'] == 'cck_table') {
foreach ($instance_value['display'] as $context => $settings) {
if (in_array($instance_value['display'][$context]['type'], array(
'first_row_header',
'without_header',
))) {
$instance_value['display'][$context]['type'] = 'cck_table_' . $instance_value['display'][$context]['type'];
}
}
}
}