You are here

function _gc_fetcher in GatherContent 8.3

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

Function for fetching, creating and updating content from GatherContent.

Parameters

int $gc_id: ID of GatherContent piece of content.

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

bool $drupal_status: Drupal status - published/unpublished.

string $node_update_method: Name of the node update method.

int|null $status: ID of status from GatherContent.

string|null $parent_menu_item: Parent menu item ID if we want to create menu item.

Return value

bool Return nid if operation was successful.

2 calls to _gc_fetcher()
gathercontent_import_process in ./gathercontent.module
Batch operation callback.
gathercontent_update_process in ./gathercontent.module
Batch operation callback.

File

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

Code

function _gc_fetcher($gc_id, $uuid, $drupal_status, $node_update_method, $status = 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::entityTypeManager()
    ->getStorage('gathercontent_operation_item')
    ->create([
    '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,
  ]);
  $mapping_id = Drupal::entityQuery('gathercontent_mapping')
    ->condition('gathercontent_project_id', $content->project_id)
    ->condition('gathercontent_template_id', $content->template_id)
    ->execute();
  if (!empty($mapping_id)) {
    $mapping = Mapping::load(reset($mapping_id));

    // If mapping exists, start mapping remote fields to local ones.
    $mapping_data = unserialize($mapping
      ->getData());
    if (empty($mapping_data)) {
      return FALSE;
    }
    $mapping_data_copy = $mapping_data;
    $first = array_shift($mapping_data_copy);
    $content_type = $mapping
      ->getContentType();
    $langcode = isset($first['language']) ? $first['language'] : Language::LANGCODE_NOT_SPECIFIED;
    $entity = gc_get_destination_node($gc_id, $node_update_method, $content_type, $langcode);
    $entity
      ->set('gc_id', $gc_id);
    $entity
      ->set('gc_mapping_id', $mapping
      ->id());
    $entity
      ->setOwnerId($user
      ->id());
    if ($entity
      ->isNew()) {
      $entity
        ->setPublished($drupal_status);
    }
    if ($entity !== FALSE) {

      /** @var \Drupal\node\NodeInterface $entity */
      try {
        $content_obj = new Content();
        $files = $content_obj
          ->getFiles($gc_id);
        $is_translatable = \Drupal::moduleHandler()
          ->moduleExists('content_translation') && \Drupal::service('content_translation.manager')
          ->isEnabled('node', $mapping
          ->getContentType());
        foreach ($content->config as $pane) {
          $is_translatable &= isset($mapping_data[$pane->name]['language']) && $mapping_data[$pane->name]['language'] != Language::LANGCODE_NOT_SPECIFIED;
          if ($is_translatable) {
            $language = $mapping_data[$pane->name]['language'];
            if (!$entity
              ->hasTranslation($language)) {
              $entity
                ->addTranslation($language);
              if ($entity
                ->isNew()) {
                $entity
                  ->getTranslation($language)
                  ->setPublished($drupal_status);
              }
            }
          }
          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'])) {
                gc_gc_process_content_pane($entity, $local_field_name, $field, $is_translatable, $language, $files);
              }
              elseif (isset($mapping_data[$pane->name]['type']) && $mapping_data[$pane->name]['type'] === 'metatag') {
                gc_gc_process_metatag_pane($entity, $local_field_name, $field, $mapping
                  ->getContentType(), $is_translatable, $language);
              }
            }
          }
        }
        if (!$is_translatable && empty($entity
          ->getTitle())) {
          $entity
            ->setTitle($content->name);
        }
        \Drupal::service('event_dispatcher')
          ->dispatch(GatherContentEvents::PRE_NODE_SAVE, new PreNodeSaveEvent($entity, $content, $files));
        $entity
          ->save();

        // Create menu link items.
        $menu_link_defaults = menu_ui_get_menu_link_defaults($entity);
        if (!(bool) $menu_link_defaults['id']) {
          if ($is_translatable) {
            $languages = $entity
              ->getTranslationLanguages();
            $original_link_id = NULL;
            foreach ($languages as $langcode => $language) {
              $localized_entity = $entity
                ->hasTranslation($langcode) ? $entity
                ->getTranslation($langcode) : NULL;
              if (!is_null($localized_entity)) {
                gc_create_menu_link($entity
                  ->id(), $localized_entity
                  ->getTitle(), $parent_menu_item, $langcode, $original_link_id);
              }
            }
          }
          else {
            gc_create_menu_link($entity
              ->id(), $entity
              ->getTitle(), $parent_menu_item);
          }
        }
        \Drupal::service('event_dispatcher')
          ->dispatch(GatherContentEvents::POST_NODE_SAVE, new PostNodeSaveEvent($entity, $content, $files));
        $operation_item->status = "Success";
        $operation_item->nid = $entity
          ->id();
        $operation_item
          ->save();
        return $entity
          ->id();
      } catch (Exception $e) {
        \Drupal::logger('gc_import')
          ->error(print_r($e, TRUE), []);
        $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;
  }
}