Skip to content
Snippets Groups Projects

Resolve #1677596 "Copy paste option"

Merged erutan requested to merge issue/tablefield-1677596:1677596-copy-paste-option into 8.x-2.x
Files
3
+ 118
0
@@ -28,6 +28,7 @@ class Tablefield extends FormElement {
'#input_type' => 'textfield',
'#rebuild' => FALSE,
'#import' => FALSE,
'#paste' => FALSE,
'#process' => [
[$class, 'processTablefield'],
],
@@ -229,6 +230,60 @@ class Tablefield extends FormElement {
];
}
if (!empty($element['#paste'])) {
// Allow user to paste data (e.g. from Excel)
$element['tablefield']['paste'] = [
'#type' => 'details',
'#title' => t('Copy & Paste'),
'#open' => FALSE,
];
$delimiters = [
'TAB' => 'TAB',
',' => 'Comma ,',
';' => 'Semicolon ;',
'|' => 'Pipe |',
'+' => 'Plus +',
':' => 'Colon :',
];
$element['tablefield']['paste']['delimiter'] = [
'#type' => 'select',
'#tree' => TRUE,
'#title' => t('Column separator'),
'#name' => 'tablefield-paste-delimiter-' . $id,
'#options' => $delimiters,
'#description' => t('Data copied from Excel will use TAB.'),
];
$element['tablefield']['paste']['data'] = [
'#type' => 'textarea',
'#tree' => TRUE,
'#name' => 'tablefield-paste-data-' . $id,
'#title' => t('Paste table data here:'),
];
$element['tablefield']['paste']['paste_import'] = [
'#type' => 'submit',
'#submit' => [[get_called_class(), 'submitCallbackRebuild']],
'#value' => t('Import & Rebuild'),
'#name' => 'tablefield-paste-' . $id,
'#attributes' => [
'class' => ['tablefield-paste'],
],
'#limit_validation_errors' => [
array_merge($parents, ['tablefield', 'reuild', 'cols']),
array_merge($parents, ['tablefield', 'rebuild', 'rows']),
],
'#ajax' => [
'callback' => 'Drupal\tablefield\Element\Tablefield::ajaxCallbackRebuild',
'progress' => ['type' => 'throbber', 'message' => NULL],
'wrapper' => 'tablefield-' . $id . '-wrapper',
'effect' => 'fade',
],
];
}
// Allow import of a csv file.
if (!empty($element['#import'])) {
$element['tablefield']['import'] = [
@@ -343,6 +398,23 @@ class Tablefield extends FormElement {
NestedArray::setValue($form_state->getStorage(), $parents, $imported_tablefield['rebuild']);
}
}
elseif (isset($triggering_element['#name']) && $triggering_element['#name'] == 'tablefield-paste-' . $id) {
// Import from pasted text.
$columnDelimiter = $form_state->getUserInput()['tablefield-paste-delimiter-' . $id];
$pastedData = $form_state->getUserInput()['tablefield-paste-data-' . $id];
$imported_tablefield = static::importPasted($columnDelimiter, $pastedData);
if ($imported_tablefield) {
$form_state->setValue($parents, $imported_tablefield);
$input = $form_state->getUserInput();
NestedArray::setValue($input, $parents, $imported_tablefield);
$form_state->setUserInput($input);
$parents[] = 'rebuild';
NestedArray::setValue($form_state->getStorage(), $parents, $imported_tablefield['rebuild']);
}
}
$form_state->setRebuild();
}
@@ -410,6 +482,52 @@ class Tablefield extends FormElement {
return FALSE;
}
/**
* Helper function to import data from pasted content.
*
* @param array $columnDelimiter
* Array $delimiters.
* @param string $pastedData
* Contents of data pasted into field.
*
* @return mixed
* Table array or FALSE.
*/
private static function importPasted($columnDelimiter, $pastedData) {
if (!empty($pastedData)) {
if ($columnDelimiter == 'TAB') {
$columnDelimiter = "\t";
}
$max_col_count = 0;
$row_count = 0;
$tablefield = [];
$rows = explode(PHP_EOL, $pastedData);
foreach ($rows as $row_index => $row) {
// Explode the current row into columns:
$cols = explode($columnDelimiter, $row);
$col_count = count($cols);
if ($col_count > 0) {
foreach ($cols as $col_index => $col) {
$tablefield['table'][$row_index][] = $col;
}
$max_col_count = $col_count > $max_col_count ? $col_count : $max_col_count;
$row_count++;
}
}
$tablefield['rebuild']['cols'] = $max_col_count;
$tablefield['rebuild']['rows'] = $row_count;
\Drupal::messenger()->addMessage(t('Successfully imported pasted data.'));
return $tablefield;
}
\Drupal::messenger()->addMessage(t('There was a problem importing pasted data.'));
return FALSE;
}
/**
* Helper function to detect and convert strings not in UTF-8 to UTF-8.
*
Loading