Creating nodes in Drupal install profiles

5 comments

One of the toughest things for me personally is coming up content for a new website. Specifically things that resemble an "about" page. I find it significantly easier if I'm given a starting point and something to work from. Once I get started I have no problem writing some decent content.

To make things a bit simpler for couples using Wedful wedding websites we decided to create default nodes with example content. This way users aren't given a blank slate when starting out. We've set up eight default pages to give them a bit of a starting point with the content on their site. Each page contains some example text or questions to give the users an idea of what to write.

To do this, we create nodes from within our Wedful install profile while the site is being generated. We also create some default users (in addition to UID 1) and we wanted the content to be owned by one of those users and not the admin account. It was also important to have the correct input formats (also created in the install profile -- more about this in another post) assigned to the created content.

In my install profile I've created a 'content' folder:

/profiles/myprofile/content

Then, for each node to be created, for example a welcome page, I create a welcome.content with what's mostly a copy of the "dev load" tab if you're using the devel module.

At the end of the install profile bootstrap (after pretty much everything else is configured) I call a function to import all the .content files. I pass any necessary information to that function including the relevant input format id and the user ids.

function myprofile_create_content($formats, $dir = 'content', $ext = 'content', $account = NULL) {  
  $path = dirname(__FILE__);
  $files = file_scan_directory($path . '/' . $dir, "\.${ext}$");
  asort($files);

  foreach ($files as $file) {
    $node = array();
    ob_start();
    include $file->filename;
    ob_end_clean();

    $node = (object) $node;
    $node->uid = $account ? $account->uid : 1;
    $node->format = $formats['Full HTML'];
    $node->field_body[0]['format'] = $formats['Full HTML'];
    node_save($node);
  }
}

Since welcome.content is PHP code itself it makes importing and saving the node into the site extremely easy. Right before I save I set the author uid and the input format to use for the content. In the future I plan on customizing the content with tokens based on what the user inputted on signup.

For convenience, I also prefixed each of my .content files with a number (01-welcome.content, 02-proposal.content, etc). I save the nodes in that order so that all sites will have matching nids.

And that's all there is to it.

Peanut Gallery

The node export module

The node export module (http://drupal.org/project/node_export) and install profile API can work well together to export nodes, it does CCK taxonomy etc too. Mostly, it just makes exporting a lot easier.

Good call

Scott Hadfield

Thanks Steven that's a good call. I haven't used node_export myself but it seems like a good way to go about this. I can't imagine it would be too hard to get it to run an import in an install profile either.

Full HTML?

Very cool, Scott - but doesn't this mean that your users need to have a certain role so they can edit the node, since the input format is "Full HTML"? Otherwise they'll get an "access denied"...or are you allowing your users the ability to use the Full HTML input format?

Yup

Scott Hadfield

That's right, because my users are using a wysiwyg (ck) it was easiest to just give them Full HTML... plus it's their website, so if they want to mess it up with HTML that's their call ;-). I suppose later I could fine tune a filter so that it only allowed the markup the wysiwyg supports, but it's not that important for now.

Images

Joao Garin

Hello,

First off, great article i was looking for something exactly like this.I wanted to know if you have any idea how to build this with images also, creating nodes and automatically assigning images for say a "image" field (not stored in node table).

I am going to investigate on this, if i come up with something i'll post it here.

Best regards,
Joao

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options