Warning message

Newsletter category Drupal Developer newsletter format has not been set to HTML. There will be no statistics recorded for this newsletter.

Drupal: Create Drupal7 node Programmatically

This CodeLet will read data from an external API, parse the JSON response & build a node of specific type. It imports taxonomies & images as well.
The fascinating thing about this CodeLet is, it can import data to multi - value node fields, including multi widget fields & different field types.

Module Info

name = Node import
description = Node import From API
core = 7.x
package = Drupal Developer

CodeLet: 

<?php

/**
* @fle
*
*/

/**
* Implemenation of hook_menu
*
* @author DruaplD
*/
function hs_Pnode_import_menu() {
$items = array();

$items['hs-import'] = array(
'type' => MENU_CALLBACK,
'title' => t('Pnode Imports'),
'page callback' => 'hs_Pnode_import_data',
'access callback' => TRUE,
);

return $items;
}

/**
* hs_Pnode_import_api_connection
*
* Connect to API
*
* @author DruaplD
*/
function hs_Pnode_import_api_connection($type, $estateId = NULL, $imageId = NULL) {
$ch = curl_init();

switch($type) {
//Here goes calls to external API using cURL
}

$result = curl_exec($ch);
curl_close($ch);
return $result;
}

/**
* hs_Pnode_import_data
*
* Import data using API
*
* @author DruaplD
*/
function hs_Pnode_import_data() {
//Perform some pre-checks, make connection to external API using cURL, import JSON & parse it to import the data to node
_Pnode_build($type, $Id, $cId);
}

/**
* _Pnode_build
*
* @author DruaplD
*/
function _Pnode_build($type, $Id, $cId) {
global $user;
$ar__Pnode = hs_Pnode_import_api_connection($type, $Id, $cId);
$om__Pnode = json_decode($ar__Pnode);

if (!empty($om__Pnode)) {

$node = new stdClass();
$node->type = 'Pnode';
$node->uid = ($user->uid > 0 ? $user->uid : 1);

//Update. If node already exists
$nid = _get_nid($estate_ID);

if (!empty($nid)) {
$node->nid = $nid;
}

node_object_prepare($node);

$node->title = $om__Pnode->baseInformation->objectAddress->streetAddress .' '. $om__Pnode->assignment->estatenumber;
$node->body['und'][0]['value'] = $om__Pnode->description->longSellingDescription;
$node->body['und'][0]['summary'] = text_summary($om__Pnode->description->longSellingDescription);

//Images -- Multi Value field
foreach ($om__Pnode->advertiseOn->imageIds as $imageId) {
$node->field_advertisement_image_id['und'][]['value'] = $imageId;
}

//Documents -- Multi Value field
foreach ($om__Pnode->advertiseOn->documents as $document) {
$node->field_advertisement_documents['und'][]['value'] = $document;
}

//Links -- Multi Value field with multi widgets
foreach ($om__Pnode->advertiseOn->links as $link) {
$k = 0;
$node->field_advertisement_links['und'][$k]['title'] = $link->text;
$node->field_advertisement_links['und'][$k]['url'] = $link->links[0];
$k++;
}

//Coordinates -- Geo field
$ss__latitude = ($om__Pnode->baseInformation->objectAddress->coordinate->latitud ?: NULL);
$ss__longitude = ($om__Pnode->baseInformation->objectAddress->coordinate->longitud ?: NULL);
if ($ss__longitude != NULL && $ss__latitude != NULL) {
$geofield = array(
'geom' => "POINT ($ss__longitude $ss__latitude)",
'geo_type' => 'point',
'lat' => $ss__latitude . "000000",
'lon' => $ss__longitude . "000000",
'left' => $ss__longitude . "000000",
'top' => $ss__latitude . "000000",
'right' => $ss__longitude . "000000",
'bottom' => $ss__latitude . "000000"
);
$node->field_coordinates['und'][0] = $geofield;
}

//Street -- Taxonomy
$street = taxonomy_get_term_by_name($om__Pnode->baseInformation->objectAddress->streetAddress);
if (empty($street[0])) {
$street = new stdClass();
$street->vid = 4;
$street->name = trim($om__Pnode->baseInformation->objectAddress->streetAddress);
taxonomy_term_save($street);
}
else {
$street = array_shift($street);
}
$node->field_Pnode_street['und'][0]['tid'] = $street->tid;

//Province -- Taxonomy
$province = taxonomy_get_term_by_name($om__Pnode->baseInformation->objectAddress->area);
if (empty($province[0])) {
$province = new stdClass();
$province->vid = 3;
$province->name = trim($om__Pnode->baseInformation->objectAddress->area);
taxonomy_term_save($province);
}
else {
$province = array_shift($province);
}
$node->field_Pnode_province['und'][0]['tid'] = $province->tid;

//City -- Taxonomy
$city = taxonomy_get_term_by_name($om__Pnode->baseInformation->objectAddress->city);
if (empty($city[0])) {
$city = new stdClass();
$city->vid = 2;
$city->name = trim($om__Pnode->baseInformation->objectAddress->city);
taxonomy_term_save($city);
}
else {
$city = array_shift($city);
}
$node->field_Pnode_city['und'][0]['tid'] = $city->tid;

//Submit node
$node = node_submit($node);
//Save node
node_save($node);

drupal_set_message(t('Pnode :node imported successfully with Nod id :nid', array(':node' => $node->title, ':nid' => $node->nid)));
}
}

By DrupalD on Tue, 30/08/2016 - 11:23am

Add comment

Log in or register to post comments
QR code for this page URL This page URL
Feedback