public static function NodeImportController::simpleNodeCreate in Simple Node Importer 8
Creates node for specified type of mapped data.
File
- src/
Controller/ NodeImportController.php, line 51
Class
- NodeImportController
- Default controller for the simple_node_importer module.
Namespace
Drupal\simple_node_importer\ControllerCode
public static function simpleNodeCreate($records, &$context) {
$user = "";
$entity_type = 'node';
$userAutoCreate = \Drupal::config('simple_node_importer.settings')
->get('simple_node_importer_allow_user_autocreate');
foreach ($records as $record) {
$batch_result['result'] = '';
if (!empty($record['uid'])) {
if (filter_var($record['uid'], FILTER_VALIDATE_EMAIL)) {
$user = \Drupal::service('snp.get_services')
->getUserByEmail($record['uid'], $userAutoCreate);
}
else {
$user = \Drupal::service('snp.get_services')
->getUserByUsername($record['uid'], $userAutoCreate);
}
}
else {
if ($userAutoCreate == 'admin') {
$user = 1;
}
elseif ($userAutoCreate == 'current') {
$user = \Drupal::currentUser();
}
else {
$batch_result['result'] = $record;
}
}
// Assigning user id to node.
if ($user && !is_int($user)) {
$uid = $user
->id();
}
else {
$uid = $user;
}
if (empty($record['title'])) {
$batch_result['result'] = $record;
}
$node_data = [
'type' => $record['type'],
'title' => !empty($record['title']) ? $record['title'] : '',
'uid' => $uid,
'status' => $record['status'] == 1 || $record['status'] == TRUE ? TRUE : FALSE,
];
$field_names = array_keys($record);
if (empty($batch_result['result'])) {
$batch_result = \Drupal::service('snp.get_services')
->checkFieldWidget($field_names, $record, $node_data, $entity_type);
}
if (!empty($batch_result['result'])) {
if (!isset($context['results']['failed'])) {
$context['results']['failed'] = 0;
}
$context['results']['failed']++;
$batch_result['result']['sni_id'] = $context['results']['failed'];
$context['results']['sni_nid'] = $record['nid'];
$context['results']['data'][] = serialize($batch_result['result']);
}
else {
$node = Node::create($batch_result);
$node
->save();
if ($node
->id()) {
if (!isset($context['results']['created'])) {
$context['results']['created'] = 0;
}
$context['results']['created']++;
}
else {
$context['results']['failed']++;
$batch_result['result']['sni_id'] = $context['results']['failed'];
$context['results']['sni_nid'] = $record['nid'];
$context['results']['data'] = $batch_result['result'];
}
}
}
}