Чтение узла из файла CSV и сохранение с помощью пакета — Drupal 7

Когда я пытаюсь сохранить узел с помощью пакетной обработки, я получаю эту ошибку:

PDOException: в drupal_write_record () (строка 7383 из C: \ wamp64 \ www \ drupal7 \ includes \ common.inc).

Вот мой полный код:

function custom_node_import_form_submit($form, &$form_state) {
$csvFile = file_load($form_state['values']['csv_file']);
$csvFilepath = drupal_realpath($csvFile->uri);
$file = fopen($csvFilepath, "r");

$batch = array(
'operations' => array(),
'finished' => 'node_import_finished',
'title' => t('Node import'),
'init_message' => t('Importing is starting...'),
'progress_message' => t('Imported @current out of @total.'),
'error_message' => t('Node importer has encountered an error.')
);

fgetcsv($file, 0, ",");
while($line = fgetcsv($file)) {
$batch['operations'][] = array('node_import_progress', array(array_map('base64_encode', $line)));
}

batch_set($batch);
batch_process('admin/node/custom-node-import');
fclose($file);
}

function node_import_progress($line, &$context) {
$line = array_map('base64_decode', $line);
saveNode($line);
$context['message'] = t('Importing %title', array('%title' => $line[0]));
}

function node_import_finished($success, $results, $operations) {
if ($success) {
drupal_set_message(t('Node importing is complete!'));
}
else {
$error_operation = reset($operations);
$message = t('An error occurred while processing %error_operation with arguments: @arguments', array(
'%error_operation' => $error_operation[0],
'@arguments' => print_r($error_operation[1], TRUE)
));
drupal_set_message($message, 'error');
}
}
function saveNode($param = []) {
global $user;

$node = new stdClass();
$node->title = $param[0];
$node->type = "article";
node_object_prepare($node);
$node->language = LANGUAGE_NONE;
$node->uid = $user->uid;
$node->status = 1;
$node->promote = 0;
$node->comment = 1;
$node->body[$node->language][]['value'] = $param[3];

$node = node_submit($node);
node_save($node);
}

Я отладил и обнаружил, что строка, которая дает эту ошибку, является последней строкой, в которой сохраняется узел. т.е. node_save ($ узел);

0

Решение

Проблема была с неанглийскими символами в CSV-файле, такими как ö, ä и т. Д.

Таким образом, решение было использовать utf8_encode () для значений:

$node->title = utf8_encode($param[0]);
$node->body[$node->language][]['value'] = utf8_encode($param[3]);

Надеюсь, это кому-нибудь поможет.

1

Другие решения

Вы также можете добавить преобразование для всех значений, например:

array_walk($param, function(&$value){
$value = mb_convert_encoding($value, 'UTF-8', mb_detect_encoding($value));
});
0

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector