I'm building a basic D7 module that fetches data from an API and then add it as a certain content type. Problem is, every node_ function is coming back as undefined. node_object_prepare, node_submit, and node_save with this error message:
Call to undefined function node_object_prepare()
The module is bare bones... info file:
name = Social Fetch
description = Fetches feeds from various social media platforms.
package = public-action
version = 1.0
core = 7.x
files[] = social_fetch.module
and the social_fetch.module...
<?php
if(isset($_GET['fetch']))
{
social_fetch();
}
function social_fetch(){
$node = new stdClass();
$node->title = 'test';
$node->type = "social_boxes";
node_object_prepare($node); // Sets some defaults. Invokes hook_prepare() and hook_node_prepare().
$node->language = LANGUAGE_NONE; // Or e.g. 'en' if locale is enabled
$node->uid = 1;
$node->status = 0; //(1 or 0): published or not
$node->promote = 0; //(1 or 0): promoted to front page
$node->comment = 0; // 0 = comments disabled, 1 = read only, 2 = read/write
$node = node_submit($node); // Prepare node for saving
node_save($node);
}
So much as I knew, node_ was part of the core and wouldn't need any dependencies but I may be wrong. I tried adding this:
module_load_include('inc', 'node', 'node.module');
but that did nothing. also tried loading the bootstrap to see if maybe this was being run before the core loaded, but that did nothing as well.
require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
Am I missing something? should this be run in a hook of some kind and if so, which?
Side Note
I am able to achieve what I need by using the db_ methods below. It just seems like an inferior way to do so since I don't actually know the anatomy of a node to know if these are the only things required to make sure the system remains stable. The kind of things the node API would have handled 100%.
$nid = db_insert('node')
->fields(array(
'title' => 'test',
'uid' => 1,
'type' => 'social_boxes',
'language' => 'und',
'status' => 0,
'comment' => 0,
'promote' => 0,
'created' => REQUEST_TIME,
'changed' => REQUEST_TIME,
))
->execute();
if($nid)
{
db_update('node') //add vid back into the new node
->fields(array(
'vid' => $nid
))
->condition('nid', $nid, '=')
->execute();
db_insert('node_revision')
->fields(array(
'nid' => $nid,
'vid' => $nid,
'uid' => 1,
'title' => $class->getText(),
'timestamp' => REQUEST_TIME,
'log' => '',
'status' => 0,
'comment' => 0,
'promote' => 0,
'sticky' => 0
))
->execute();
}