You are here

function _gc_fetcher in GatherContent 8

Same name and namespace in other branches
  1. 8.3 gathercontent.module \_gc_fetcher()

Helper function for fetching data from GatherContent.

Parameters

int $gc_id: ID of GatherContent piece of content.

object|null $entity: Object of entity, if we are updating exiting content.

Return value

int|FALSE Return nid if operation was successful.

2 calls to _gc_fetcher()
gc_import_process in ./gathercontent.module
Batch operation callback.
gc_update_process in ./_old.gc.module
Batch operation callback.

File

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

Code

function _gc_fetcher($gc_id, $uuid, $status = NULL, $entity = NULL, $parent_menu_item = NULL) {
  $user = \Drupal::currentUser();
  $tsid = NULL;
  $content_obj = new Content();
  $content = $content_obj
    ->getContent($gc_id);
  if (!empty($status)) {
    $status_obj = new Project();
    $status = $status_obj
      ->getStatus($content->project_id, $status);
  }
  $temp_obj = new Template();
  $template = $temp_obj
    ->getTemplate($content->template_id);
  $operation_item = \Drupal::entityManager()
    ->getStorage('gc_operation_item')
    ->create(array(
    'operation_uuid' => $uuid,
    'item_status' => !empty($status) ? $status->name : $content->status->data->name,
    'item_status_color' => !empty($status) ? $status->color : $content->status->data->color,
    'template_name' => $template->name,
    'item_name' => $content->name,
    'gc_id' => $gc_id,
  ));

  // Check if mapping exists.
  $query = new EntityFieldQuery();
  if (is_null($entity)) {
    $result = $query
      ->entityCondition('entity_type', 'gc_mapping')
      ->propertyCondition('gc_project_id', $content->project_id)
      ->propertyCondition('gc_template_id', $content->template_id)
      ->execute();
  }
  else {
    $result = $query
      ->entityCondition('entity_type', 'gc_mapping')
      ->propertyCondition('id', $entity->gc_mapping_id)
      ->execute();
  }
  if (isset($result['gc_mapping'])) {

    // If mapping exists, start mapping remote fields to local ones.
    $mapping_ids = array_keys($result['gc_mapping']);
    $entities = \Drupal::entityManager()
      ->getStorage('gc_mapping');
    $mapping = reset($entities);
    $mapping_data = unserialize($mapping->data);
    if (empty($mapping_data)) {
      return FALSE;
    }
    $mapping_data_copy = $mapping_data;
    $first = array_shift($mapping_data_copy);
    if (is_null($entity)) {
      $entity = \Drupal::entityManager()
        ->getStorage('node')
        ->create(array(
        'type' => $mapping->content_type,
        'gc_mapping_id' => $mapping->id,
        'gc_id' => $content->id,
        'uid' => $user->uid,
        'language' => isset($first['language']) ? $first['language'] : \Drupal\Core\Language\Language::LANGCODE_NOT_SPECIFIED,
      ));
      $entity
        ->save();
    }
    if ($entity !== FALSE) {
      $node = entity_metadata_wrapper('node', $entity);
      $metatags = array();
      $_title_is_mapped = FALSE;
      try {
        foreach ($content->config as $pane) {
          $is_translatable = \Drupal::moduleHandler()
            ->moduleExists('entity_translation') && entity_translation_node_supported_type($mapping->content_type) && isset($mapping_data[$pane->name]['language']) && $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 (isset($mapping_data[$pane->name]['type']) && $mapping_data[$pane->name]['type'] === 'content' || !isset($mapping_data[$pane->name]['type'])) {
                gc_gc_process_content_pane($node, $gc_id, $local_field_name, $field, $mapping->content_type, $is_translatable, $language, $parent_menu_item, $entity->nid, $tsid, $_title_is_mapped);
              }
              elseif (isset($mapping_data[$pane->name]['type']) && $mapping_data[$pane->name]['type'] === 'metatag') {
                gc_gc_process_metatag_pane($node, $metatags, $gc_id, $local_field_name, $field, $mapping->content_type, $is_translatable, $language);
              }
            }
            else {
              $operation_item->status = "Operation failed: Template fields mismatch.";
              $operation_item
                ->save();
            }
          }

          // We need to create new translation if is translatable.
          if ($is_translatable && (!isset($node->language) || empty($node->language))) {
            $node->language
              ->set($language);
            $node
              ->save(TRUE);
          }
          elseif ($is_translatable && (isset($node->language) || !empty($node->language))) {
            $handler = entity_translation_get_handler('node', $entity);
            $translation = array(
              'translate' => 0,
              'status' => 1,
              'language' => $language,
              'source' => $language === $node->language
                ->value() ? '' : $node->language
                ->value(),
            );
            $handler
              ->setTranslation($translation, $node
              ->value());
          }
        }
        if (!$is_translatable && !$_title_is_mapped) {
          $node->title
            ->set($content->name);
        }
        $node
          ->save(TRUE);
        if (\Drupal::moduleHandler()
          ->moduleExists('metatag') && metatag_entity_supports_metatags('node', $mapping->content_type)) {
          module_load_include('module', 'metatag');
          metatag_metatags_save('node', $entity->nid, $entity->vid, $metatags);
        }
        $operation_item->status = "Success";
        $operation_item->nid = $entity->nid;
        $operation_item
          ->save();
        return $entity->nid;
      } catch (\Exception $e) {
        \Drupal::logger('gc_import')
          ->error(print_r($e, TRUE), array());
        $operation_item->status = "Operation failed: {$e->getMessage}()";
        $operation_item
          ->save();
        return FALSE;
      }
    }
    else {
      $operation_item->status = "System error, please contact you administrator.";
      $operation_item
        ->save();
      return FALSE;
    }
  }
  else {
    $operation_item->status = "Operation failed: Template not mapped.";
    $operation_item
      ->save();
    return FALSE;
  }
}