You are here

function gathercontent_upload_process in GatherContent 8.3

Same name and namespace in other branches
  1. 8.5 gathercontent_upload/gathercontent_upload.module \gathercontent_upload_process()
  2. 7.3 gathercontent.module \gathercontent_upload_process()

Upload batch operation callback.

Parameters

\Drupal\node\NodeInterface $entity: Object of entity we want to upload.

string $uuid: UUID of \Drupal\gathercontent\Entity\Operation entity.

array $context: Context of operation.

1 string reference to 'gathercontent_upload_process'
ContentUploadConfirmForm::submitForm in src/Form/ContentUploadConfirmForm.php
Form submission handler.

File

./gathercontent.module, line 962
Main module file for GatherContent module.

Code

function gathercontent_upload_process(NodeInterface $entity, $uuid, array &$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 = Mapping::load($entity
    ->get('gc_mapping_id')
    ->getValue());
  $tmp_obj = new Template();
  $remote_template = $tmp_obj
    ->getTemplate($mapping
    ->getGathercontentTemplateId());
  $cont_obj = new Content();
  $remote_node = $cont_obj
    ->getContent($entity
    ->get('gc_id')
    ->getValue());
  $config = $remote_node->config;
  $mapping_data = unserialize($mapping
    ->getData());
  $operation_item = \Drupal::entityTypeManager()
    ->getStorage('gathercontent_operation_item')
    ->create([
    '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
      ->get('gc_id'),
    'nid' => $entity
      ->id(),
  ]);
  if ($remote_template->config == unserialize($mapping
    ->getTemplate())->config) {
    try {
      foreach ($config as &$pane) {
        $is_translatable = \Drupal::moduleHandler()
          ->moduleExists('content_translation') && \Drupal::service('content_translation.manager')
          ->isEnabled('node', $mapping
          ->getContentType()) && isset($mapping_data[$pane->name]['language']) && $mapping_data[$pane->name]['language'] != Language::LANGCODE_NOT_SPECIFIED;
        if ($is_translatable) {
          $language = $mapping_data[$pane->name]['language'];
        }
        else {
          $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 (isset($mapping_data[$pane->name]['type']) && $mapping_data[$pane->name]['type'] === 'content' || !isset($mapping_data[$pane->name]['type'])) {
              $field_info = FieldConfig::loadByName('node', $entity
                ->bundle(), $local_field_name);
              if (!is_null($field_info)) {
                $is_translatable = $is_translatable && $field_info
                  ->isTranslatable();
              }
              switch ($field->type) {
                case 'files':

                  // There is currently no API for manipulating with files.
                  break;
                case 'choice_radio':
                  $option_names = [];
                  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 = $entity
                      ->getTranslation($language)->{$local_field_name}->value;
                  }
                  else {
                    $selected = $entity->{$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 = $entity
                      ->getTranslation($language)->{$local_field_name}->value;
                  }
                  else {
                    $selected = $entity->{$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') {
                    if ($is_translatable) {
                      $field->value = $entity
                        ->getTranslation($language)
                        ->getTitle();
                    }
                    else {
                      $field->value = $entity
                        ->getTitle();
                    }
                  }
                  else {
                    if ($is_translatable) {
                      $field->value = $entity
                        ->getTranslation($language)->{$local_field_name}->value;
                    }
                    else {
                      $field->value = $entity->{$local_field_name}->value;
                    }
                  }
                  break;
              }
            }
            elseif ($mapping_data[$pane->name]['type'] === 'metatag') {
              if (\Drupal::moduleHandler()
                ->moduleExists('metatag') && check_metatag($entity
                ->getType())) {
                $metatag_fields = get_metatag_fields($entity
                  ->getType());
                foreach ($metatag_fields as $metatag_field) {
                  if ($is_translatable) {
                    $field->value = $entity
                      ->getTranslation($language)->{$metatag_field}
                      ->value();
                  }
                  else {
                    $field->value = $entity->{$metatag_field}
                      ->value();
                  }
                }
              }
            }
          }
          else {
            $operation_item->status = "System error, please contact you administrator.";
            $operation_item
              ->save();
          }
        }
      }
      $event = \Drupal::service('event_dispatcher')
        ->dispatch(GatherContentEvents::PRE_NODE_UPLOAD, new PreNodeUploadEvent($entity, $config));

      /** @var \Drupal\gathercontent\Event\PreNodeUploadEvent $event */
      $config = $event
        ->getGathercontentValues();
      if ($cont_obj
        ->postContent($entity
        ->get('gc_id')
        ->getValue(), $config)) {
        $operation_item->status = "Success";
        $operation_item
          ->save();
        \Drupal::service('event_dispatcher')
          ->dispatch(GatherContentEvents::POST_NODE_UPLOAD, new PostNodeUploadEvent($entity, $config));
      }
      else {
        $operation_item->status = 'Mapping doesn\'t match';
        $operation_item
          ->save();
      }
    } catch (\Exception $e) {
      \Drupal::logger('gc_upload')
        ->error(print_r($e, TRUE), []);
      $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;
}