function tablefield_field_widget_form in TableField 7
Same name and namespace in other branches
- 7.3 tablefield.module \tablefield_field_widget_form()
- 7.2 tablefield.module \tablefield_field_widget_form()
Implements hook_widget_form().
File
- ./
tablefield.module, line 228 - This module provides a set of fields that can be used to store tabular data with a node. The implementation uses a custom CCK widget.
Code
function tablefield_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
drupal_add_css(drupal_get_path('module', 'tablefield') . '/tablefield.css');
$element['#type'] = 'tablefield';
$form['#attributes']['enctype'] = 'multipart/form-data';
// Establish a list of saved/submitted/default values
if (isset($form_state['clicked_button']['#value']) && $form_state['clicked_button']['#name'] == 'tablefield_rebuild_' . $field['field_name'] . '_' . $delta) {
// Rebuilding table rows/cols
$default_value = tablefield_rationalize_table($form_state['tablefield_rebuild'][$field['field_name']][$langcode][$delta]['tablefield']);
}
elseif (isset($form_state['clicked_button']['#value']) && $form_state['clicked_button']['#name'] == 'tablefield-import-button-' . $field['field_name'] . '-' . $delta) {
// Importing CSV data
tablefield_import_csv($form, $form_state);
$default_value = tablefield_rationalize_table($form_state['input'][$field['field_name']][$langcode][$delta]['tablefield']);
}
elseif ($form_state['submitted'] && isset($items[$delta]) && isset($items[$delta]['tablefield'])) {
// A form was submitted
$default_value = tablefield_rationalize_table($items[$delta]['tablefield']);
}
elseif (isset($items[$delta]['value'])) {
// Default from database (saved node)
$default_value = tablefield_rationalize_table(unserialize($items[$delta]['value']));
}
else {
// After the first table, we don't want to populate the values in the table
if ($delta > 0) {
tablefield_delete_table_values($instance['default_value'][0]['tablefield']);
}
// Get the widget default value
$default_value = tablefield_rationalize_table($instance['default_value'][0]['tablefield']);
$default_count_cols = isset($items[0]['tablefield']['rebuild']['count_cols']) ? $items[0]['tablefield']['rebuild']['count_cols'] : 5;
$default_count_rows = isset($items[0]['tablefield']['rebuild']['count_cols']) ? $items[0]['tablefield']['rebuild']['count_cols'] : 5;
}
if (!empty($instance['description'])) {
$help_text = $instance['description'];
}
else {
$help_text = t('The first row will appear as the table header. Leave the first row blank if you do not need a header.');
}
$element['tablefield'] = array(
'#title' => $element['#title'],
'#description' => $help_text,
'#attributes' => array(
'id' => 'form-tablefield-' . $field['field_name'] . '-' . $delta,
'class' => array(
'form-tablefield',
),
),
'#type' => 'fieldset',
'#tree' => TRUE,
'#collapsible' => FALSE,
'#prefix' => '<div id="tablefield-' . $field['field_name'] . '-' . $delta . '-wrapper">',
'#suffix' => '</div>',
);
// Give the fieldset the appropriate class if it is required
if ($element['#required']) {
$element['tablefield']['#title'] .= ' <span class="form-required" title="' . t('This field is required') . '">*</span>';
}
$arg0 = arg(0);
if ($arg0 == 'admin') {
$element['tablefield']['#description'] = t('This form defines the table field defaults, but the number of rows/columns and content can be overridden on a per-node basis. The first row will appear as the table header. Leave the first row bland if you do not need a header.');
}
// Determine how many rows/columns are saved in the data
if (!empty($default_value)) {
$count_rows = count($default_value);
$count_cols = 0;
foreach ($default_value as $row) {
$temp_count = count($row);
if ($temp_count > $count_cols) {
$count_cols = $temp_count;
}
}
}
else {
$count_rows = count($default_value);
$count_cols = isset($default_count_cols) ? $default_count_cols : 0;
$count_rows = isset($default_count_rows) ? $default_count_rows : 0;
}
// Override the number of rows/columns if the user rebuilds the form.
if (isset($form_state['clicked_button']['#value']) && $form_state['clicked_button']['#name'] == 'tablefield_rebuild_' . $field['field_name'] . '_' . $delta) {
$count_cols = $form_state['tablefield_rebuild'][$field['field_name']][$langcode][$delta]['tablefield']['rebuild']['count_cols'];
$count_rows = $form_state['tablefield_rebuild'][$field['field_name']][$langcode][$delta]['tablefield']['rebuild']['count_rows'];
drupal_set_message(t('Table structure rebuilt.'), 'status', FALSE);
}
// Render the form table
$element['tablefield']['a_break'] = array(
'#markup' => '<table>',
);
for ($i = 0; $i < $count_rows; $i++) {
$zebra = $i % 2 == 0 ? 'even' : 'odd';
$element['tablefield']['b_break' . $i] = array(
'#markup' => '<tr class="tablefield-row-' . $i . ' ' . $zebra . '">',
);
for ($ii = 0; $ii < $count_cols; $ii++) {
$instance_default = isset($instance['default_value'][$delta]) ? $instance['default_value'][$delta]['tablefield']["cell_{$i}_{$ii}"] : array();
if (!empty($instance_default) && !empty($field['settings']['lock_values']) && $arg0 != 'admin') {
// The value still needs to be send on every load in order for the
// table to be saved correctly.
$element['tablefield']['cell_' . $i . '_' . $ii] = array(
'#type' => 'value',
'#value' => $instance_default,
);
// Display the default value, since it's not editable.
$element['tablefield']['cell_' . $i . '_' . $ii . '_display'] = array(
'#type' => 'item',
'#title' => $instance_default,
'#prefix' => '<td style="width:' . floor(100 / $count_cols) . '%">',
'#suffix' => '</td>',
);
}
else {
$cell_default = isset($default_value[$i][$ii]) ? $default_value[$i][$ii] : '';
$element['tablefield']['cell_' . $i . '_' . $ii] = array(
'#type' => 'textfield',
'#maxlength' => 2048,
'#size' => 0,
'#attributes' => array(
'id' => 'tablefield_' . $delta . '_cell_' . $i . '_' . $ii,
'class' => array(
'tablefield-row-' . $i,
'tablefield-col-' . $ii,
),
'style' => 'width:100%',
),
'#default_value' => empty($field_value) ? $cell_default : $field_value,
'#prefix' => '<td style="width:' . floor(100 / $count_cols) . '%">',
'#suffix' => '</td>',
);
}
}
$element['tablefield']['c_break' . $i] = array(
'#markup' => '</tr>',
);
}
$element['tablefield']['t_break' . $i] = array(
'#markup' => '</table>',
);
// Allow the user to add more rows/columns
$element['tablefield']['rebuild'] = array(
'#type' => 'fieldset',
'#tree' => TRUE,
'#title' => t('Change number of rows/columns.'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$element['tablefield']['rebuild']['count_cols'] = array(
'#title' => t('How many Columns'),
'#type' => 'textfield',
'#size' => 5,
'#prefix' => '<div class="clearfix">',
'#suffix' => '</div>',
'#value' => $count_cols,
);
$element['tablefield']['rebuild']['count_rows'] = array(
'#title' => t('How many Rows'),
'#type' => 'textfield',
'#size' => 5,
'#prefix' => '<div class="clearfix">',
'#suffix' => '</div>',
'#value' => $count_rows,
);
$element['tablefield']['rebuild']['rebuild'] = array(
'#type' => 'button',
'#validate' => array(),
'#limit_validation_errors' => array(),
'#executes_submit_callback' => TRUE,
'#submit' => array(
'tablefield_rebuild_form',
),
'#value' => t('Rebuild Table'),
'#name' => 'tablefield_rebuild_' . $field['field_name'] . '_' . $delta,
'#attributes' => array(
'class' => array(
'tablefield-rebuild',
),
),
'#ajax' => array(
'callback' => 'tablefield_rebuild_form_ajax',
'wrapper' => 'tablefield-' . $field['field_name'] . '-' . $delta . '-wrapper',
'effect' => 'fade',
'event' => 'click',
),
);
// Allow the user to import a csv file
$element['tablefield']['import'] = array(
'#type' => 'fieldset',
'#tree' => TRUE,
'#title' => t('Import from CSV'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$element['tablefield']['import']['tablefield_csv_' . $field['field_name'] . '_' . $delta] = array(
'#title' => 'File upload',
'#type' => 'file',
);
$element['tablefield']['import']['rebuild_' . $field['field_name'] . '_' . $delta] = array(
'#type' => 'button',
'#validate' => array(),
'#limit_validation_errors' => array(),
'#executes_submit_callback' => TRUE,
'#submit' => array(
'tablefield_rebuild_form',
),
'#value' => t('Upload CSV'),
'#name' => 'tablefield-import-button-' . $field['field_name'] . '-' . $delta,
'#attributes' => array(
'class' => array(
'tablefield-rebuild',
),
),
'#ajax' => array(
'callback' => 'tablefield_rebuild_form_ajax',
'wrapper' => 'tablefield-' . $field['field_name'] . '-' . $delta . '-wrapper',
'effect' => 'fade',
'event' => 'click',
),
);
// Allow the user to select input filters
if (!empty($field['settings']['cell_processing'])) {
$element['#base_type'] = $element['#type'];
$element['#type'] = 'text_format';
$element['#format'] = isset($items[$delta]['format']) ? $items[$delta]['format'] : NULL;
}
return $element;
}