0

I'm looking for a check-list style explanation of how I create this custom content type if possible.

I'm scraping data from an external website (it works flawlessly), I store it in my own database table (hook_schema(), etc.), and I display it in my config page as a table. The data gets re-scraped on cron using the queue API built into Drupal.

All this works fine. Now, my next step is to have this data turned into nodes as a custom content type. I will never need to create nodes of this content type by hand through the 'Add Content' menu because all the nodes should be generated by my module's scraping/parsing function, but the content should be searchable with Drupal's built in search. This is a bit more of a challenge. I have multiple fields (10-ish) I want to sort my data into, all different sections of the pages I'm scraping and I want to keep them separated properly so I can theme the content with a tpl file. The fields are all text, mostly HTML, some are titles/subtitles with no HTML, and a few Unix timestamps.

This has to all be done programmatically because this module might be deployed to other websites in the future, and this should not require configuring to work.

1 Answer 1

1

The first step is to create the content type and fields with the built-in UI of Drupal core. There are then several ways to export the configurations into code, but the simplest is probably Features, which will export a fully-functional module for you including all fields and content types you select.

To get your content into nodes of your content type, I'd suggest you create one node with the UI, use devel to look at the structure, then create the same object structure in code, which would look something like this:

$node = new StdClass;
$node->type = 'content_type;
$node->field_something = array(
  'und' => array(
    array(
      'value' => 'some value',
    ),
  ),
);
node_save($node);

Drupal's search defaults to searching nodes of all types, so that part should just work. Same with theming. You can use permissions (also exportable via Features) to remove the content type from the "Add content" list.

3
  • How can I use the exported code without requiring Features? I don't want my module to require other modules as much as possible. It needs to just work out of the box.
    – MaxGhost
    Aug 12, 2011 at 18:50
  • Nevermind on the previous point, I think I'll just have to deal with it. How can I theme the output? Is there a specific way to do it for custom content types?
    – MaxGhost
    Aug 12, 2011 at 19:52
  • Custom content types are no different than any other content type for theming. See: drupal.org/node/17565 Aug 16, 2011 at 17:31

Your Answer

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

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