You are here

function tablefield_import_pasted in TableField 7.2

Same name and namespace in other branches
  1. 7.3 tablefield.module \tablefield_import_pasted()

Helper function to import pasted data.

Parameters

array $form: The form structure.

array $form_state: The current state of the form.

string $langcode: The language associated with the form items.

array $tablefield_parents: The parents of the tablefield element.

1 call to tablefield_import_pasted()
tablefield_field_widget_form in ./tablefield.module
Implements hook_widget_form().

File

./tablefield.module, line 1251
Provides a set of fields that can be used to store tabular data with a node.

Code

function tablefield_import_pasted($form, &$form_state, $langcode, $pasted_form_field_name, $tablefield_parents) {
  $data = $form_state['input']['data'][$pasted_form_field_name];
  if (!empty($data)) {

    // Empty the current table.
    tablefield_delete_table_values(drupal_array_get_nested_value($form_state['values'], $tablefield_parents));
    tablefield_delete_table_values(drupal_array_get_nested_value($form_state['input'], $tablefield_parents));

    // Get the delimiter.
    $col_delimiter = $form_state['input']['delimiter'][$pasted_form_field_name];
    if ($col_delimiter == 'TAB') {
      $col_delimiter = "\t";
    }

    // Populate table with pasted values.
    $max_col_count = 0;
    $row_count = 0;
    $imported_tablefield = array();
    $rows = explode(PHP_EOL, $data);
    foreach ($rows as $row_id => $row) {

      // Explode the current row into columns:
      $cols = explode($col_delimiter, $row);
      $col_count = count($cols);
      if ($col_count > 0) {
        foreach ($cols as $col_id => $col) {
          $imported_tablefield['row_' . $row_count]['col_' . $col_id] = $col;
        }
        $max_col_count = $col_count > $max_col_count ? $col_count : $max_col_count;
        $row_count++;
      }
    }

    // Rebuild the table if necessary, and save.
    $imported_tablefield['rebuild'] = array(
      'count_cols' => $max_col_count,
      'count_rows' => $row_count,
    );
    drupal_array_set_nested_value($form_state['values'], $tablefield_parents, $imported_tablefield);
    drupal_array_set_nested_value($form_state['input'], $tablefield_parents, $imported_tablefield);
    drupal_set_message(t('Successfully imported pasted data.'));
  }
  else {
    drupal_set_message(t('There was a problem importing pasted data.'));
  }
}