2

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:

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

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

<?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' => $node,
        'data' +> $data,

    );
    return $output;

}

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

Thanks

2 Answers 2

2

I would suggest using the entity method as opposed to the node class method:

$node = entity_create('node', array('type' => 'content_type'));
$entity = entity_metadata_wrapper('node',$node);

 //Set properties
 $entity->author = $user->uid; // Author
 $entity->title->set("String");  //Title
 $entity->field_name->set("String"); //Textfield
 $entity->body->set(array('value' => "String or HTML")); //Body   
$entity->save();

Much cleaner and preferred way.

2
  • The "entity method" is from the Entity API correct? And - I'm not creating a new entity ... just adding a content type to the node ? All those nodes, entity types, entities, fields ... has my head spinning a little. Smile.
    – thornomad
    Commented Feb 4, 2014 at 23:27
  • Nodes are entities and you will need the entity api to do this. So in my example the "content_type" will be the machine name of the node you created, example "article" Commented Feb 5, 2014 at 17:03
2

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->type = 'your_content_type';
$node->status = 1;
$node->uid = 1;
$node->title = 'my node';
$node->created = REQUEST_TIME;
$node->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->nid

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

1
  • How does the $node->nid end up getting passed through the renderer so Drupal knows it's a node at output? Thanks for this.
    – thornomad
    Commented Feb 4, 2014 at 23:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.