You are here

public static function GatherContentPages::createNodes in GatherContent 7

Fetch the selected pages from GatherContent, and create nodes.

Parameters

array $page_ids: GatherContent page identifiers.

string $content_type: Which type of content to create.

1 call to GatherContentPages::createNodes()
gathercontent_import_pages_submit in ./gathercontent.module
Form submit handler.

File

./gathercontent_pages.inc, line 18
Class to fetch pages from GatherContent and turn them into Drupal nodes.

Class

GatherContentPages
@file Class to fetch pages from GatherContent and turn them into Drupal nodes.

Code

public static function createNodes($page_ids, $content_type) {
  foreach ($page_ids as $id) {
    $gc_page_id = substr($id, 11);

    // Get the page from GatherContent.
    $gc_page = gathercontent_get_command('get_page', array(
      'id' => $gc_page_id,
    ));

    // Get the actual page.
    // @GC: Why is this an array inside an object, and not just an array?
    $gc_page = $gc_page->page[0];
    $body_content = '';

    // Get all the content fields.
    foreach ($gc_page->custom_field_values as $field_name => $field_value) {
      $body_content .= $field_value;
    }

    // Prepare creation date.
    $created = strtotime($gc_page->created_at);
    $node = new stdClass();

    // Create the node(s).
    // @TODO: Move this to a batch operation?
    $node->title = $gc_page->name;
    $node->type = $content_type;
    $node->language = LANGUAGE_NONE;
    $node->body[$node->language][] = array(
      'value' => $body_content,
      'format' => 'full_html',
    );
    node_object_prepare($node);
    $node->created = $created;

    // Allow other modules to map GatherContent fields to Drupal fields
    // by implementing hook_gathercontent_presave().
    module_invoke_all('gathercontent_presave', $node, $gc_page);
    node_save($node);
    if ($node->nid) {
      drupal_set_message(t('%node_title created successfully.', array(
        '%node_title' => $node->title,
      )));
    }
    else {
      drupal_set_message(t('Something went wrong while creating %node_title:', array(
        '%node_title' => $node->title . ' ',
      )), 'error');
    }
  }
}