function gc_upload_process in GatherContent 8
Batch operation callback.
We are doing upload operation here.
1 string reference to 'gc_upload_process'
- gc_views_vbo_upload in ./
_old.gc.module - @TODO: function comment!!!
File
- ./
_old.gc.module, line 560
Code
function gc_upload_process($entity, $uuid, &$context) {
// 1. Load template from remote
// 2. Compare local and remote template
// 3. If templates are same, load node from remote.
// 4. Set values based on mapping.
$mapping = \Drupal::entityManager()
->getStorage('gc_mapping', array(
$entity->gc_mapping_id,
));
$mapping = reset($mapping);
$tmp_obj = new GatherContent\Template();
$remote_template = $tmp_obj
->getTemplate($mapping->gc_template_id);
$cont_obj = new GatherContent\Content();
$remote_node = $cont_obj
->getContent($entity->gc_id);
$config = $remote_node->config;
$mapping_data = unserialize($mapping->data);
$operation_item = \Drupal::entityManager()
->getStorage('gc_operation_item')
->create(array(
'operation_uuid' => $uuid,
'item_status' => $remote_node->status->data->name,
'item_status_color' => $remote_node->status->data->color,
'template_name' => $remote_template->name,
'item_name' => $remote_node->name,
'gc_id' => $entity->gc_id,
'nid' => $entity->nid,
));
if ($remote_template->config == unserialize($mapping->template)->config) {
$node = entity_metadata_wrapper('node', $entity);
try {
foreach ($config as &$pane) {
$is_translatable = \Drupal::moduleHandler()
->moduleExists('entity_translation') && entity_translation_node_supported_type($mapping->content_type) && $mapping_data[$pane->name]['language'] != \Drupal\Core\Language\Language::LANGCODE_NOT_SPECIFIED;
if ($is_translatable) {
$language = $mapping_data[$pane->name]['language'];
}
else {
$language = \Drupal\Core\Language\Language::LANGCODE_NOT_SPECIFIED;
}
foreach ($pane->elements as &$field) {
if (isset($mapping_data[$pane->name]['elements'][$field->name]) && !empty($mapping_data[$pane->name]['elements'][$field->name])) {
$local_field_name = $mapping_data[$pane->name]['elements'][$field->name];
if ($mapping_data[$pane->name]['type'] === 'content') {
$field_info = field_info_field($local_field_name);
$is_translatable = $is_translatable && $field_info['translatable'];
switch ($field->type) {
case 'files':
// There is currently no API for manipulating with files.
break;
case 'choice_radio':
$option_names = array();
foreach ($field->options as &$option) {
// Set selected to false for each option.
$option->selected = FALSE;
$option_names[] = $option->name;
}
// Fetch local selected option.
if ($is_translatable) {
$selected = $node
->language($language)->{$local_field_name}
->value();
}
else {
$selected = $node->{$local_field_name}
->value();
}
if (!in_array($selected, $option_names)) {
// If it's other, then find that option in remote.
foreach ($field->options as &$option) {
if (isset($option->value)) {
$option->selected = TRUE;
$option->value = $selected;
}
}
}
else {
// If it's checkbox, find it by remote option name,
// which should be same.
foreach ($field->options as &$option) {
if ($option->name == $selected) {
$option->selected = TRUE;
}
}
}
break;
case 'choice_checkbox':
foreach ($field->options as &$option) {
// Set selected to false for each option.
$option->selected = FALSE;
}
// Fetch local selected option.
if ($is_translatable) {
$selected = $node
->language($language)->{$local_field_name}
->value();
}
else {
$selected = $node->{$local_field_name}
->value();
}
// If it's checkbox, find it by remote option name,
// which should be same.
foreach ($field->options as &$option) {
if (isset($selected[$option->name])) {
$option->selected = TRUE;
}
}
break;
case 'section':
// We don't upload this because this field shouldn't be
// edited.
break;
default:
if ($local_field_name === 'title') {
$field->value = $node->title
->value();
}
else {
if ($is_translatable) {
if (title_field_replacement_enabled('node', $mapping->content_type, 'title') && title_field_replacement_info('node', 'title')['field']['field_name'] === $local_field_name) {
$field->value = $node
->language($language)->{$local_field_name}
->value();
}
else {
$field->value = $node
->language($language)->{$local_field_name}->value
->value();
}
}
else {
$field->value = $node->{$local_field_name}->value
->value();
}
}
break;
}
}
elseif ($mapping_data[$pane->name]['type'] === 'metatag') {
if (\Drupal::moduleHandler()
->moduleExists('metatag') && metatag_entity_supports_metatags('node', $mapping->content_type)) {
if ($is_translatable) {
$field->value = $node
->language($language)->metatags->{$local_field_name}
->value();
}
else {
$field->value = $node->metatags->{$local_field_name}
->value();
}
}
else {
throw new Exception("Metatag module not enabled or entity doesn't support\nmetatags while trying to map values woth metatag content.");
}
}
}
else {
$operation_item->status = "System error, please contact you administrator.";
$operation_item
->save();
}
}
}
if ($cont_obj
->postContent($entity->gc_id, $config)) {
$operation_item->status = "Success";
$operation_item
->save();
}
else {
$operation_item->status = 'Mapping doesn\'t match';
$operation_item
->save();
}
} catch (\Exception $e) {
\Drupal::logger('gc_import')
->error(print_r($e, TRUE), array());
$operation_item->status = 'Mapping doesn\'t match';
$operation_item
->save();
}
}
else {
$operation_item->status = 'Mapping doesn\'t match';
$operation_item
->save();
}
$context['results']['uuid'] = $uuid;
}