Get or Create a Node in a Custom Module This is my first time doing any custom module work in Drupal and need some advice.  I have a django background.

I am accessing data on an external non-Drupal server to display on the Drupal site.  I think I want to create a custom field type that holds the unique integer mapped to the data's ID on the external site.  


  1) user visits /mymodule/1234 where 1234 maps to the external data
  source (not the drupal node)
  2) module performs a kind of
  get_or_create and either gets the node that matches the unique ID or
  creates one
  3) the user will see get a node returned to them (can
  handle comments, statistics, etc) that also displays the external data
  (which I will query via REST)


I'm basically asking for advice if this approach is tenable in Drupal and how to implement the get_or_create part of this if it is.

what I can imagine so far

First, custom module and a url handler:

&lt;?php
function mymodule_menu() {
    $items['mymodule'] = array (
        'testing' =&gt; 'I am just testing',
        'page callback' =&gt; 'mymodule_view',
        'access callback' =&gt; TRUE,
        );
    return $items;
}


Then a custom view function to do the heavy lifting when user visits /mymodule/1234:

&lt;?php
function mymodule_view($data_id = 0) {

    // custom REST function to get the data via the $data_id
    $data = get_my_data_cleverly($data_id);

    if (!$data) {
        //return 404
    }

    // not sure about this part
    get_or_create_node($data_id);

    // then return output to theme combining both node and $data
    // this is an oversimplified render array output that doesn't
    // work mostly because I don't understnand how Drupal does this yet
    $output =  array(
        'node' =&gt; $node,
        'data' +&gt; $data,

    );
    return $output;

}


The result would be a node, with additional non-node information passed to the template.  

Thanks
Create or use an existing content type for your nodes.
Add a field (field_ext_id) on the content type to track your external content id.
When building your page, use EntityFieldQuery (note that it may have performance implications on very large sites) to look for a node with field_ext_id = your id.
If no node found, create one:

$node = new stdClass;
$node-&gt;type = 'your_content_type';
$node-&gt;status = 1;
$node-&gt;uid = 1;
$node-&gt;title = 'my node';
$node-&gt;created = REQUEST_TIME;
$node-&gt;updated = REQUEST_TIME;

// set your other field values (including field_ext_id), as necessary

node_save($node);

// your new node's id is now available as $node-&gt;nid


For working with fields, have a look at Rendering Drupal 7 Fields The Right Way and Entity Metadata Wrapper.