How to Build a Drupal Social Network
Part 3: Collecting User Contributed Content
Set up your Content Types and Permissions
Begin by thinking about the type of content your users will be sharing with each-other, and the types of things you want them to contribute to the site. These will be your content types. Typically, it's something like:
- Walls/status posts
- Photos
- Videos
Show a Node Create Form
Now you have your content type set up, you should be able to log in as a regular user and visit node/add/
Here's the code to show a node create form. For this example we're injecting a form to create nodes of type "photo". You can put this anywhere php is allowed (how about a block, or a page?). This one exposes the form for a content type called "photo"
$node = new stdClass();
$node->type = 'photo';
$node->title = "Check it out, you can even set a default title";
$node->field_album[0]['value'] = "Or a default CCK field";
module_load_include('inc', 'node', 'node.pages');
$form = drupal_get_form('photo_node_form', $node);
echo $form;
If you're curious, here's how you edit an existing node. Could be useful. In this example we're editing node 15.
$node = node_load(15);
$type = $node->type;
module_load_include('inc', 'node', 'node.pages');
$form = drupal_get_form($node->type.'_node_form', $node);
echo $form;
Maybe you could do something clever like provide a node edit form on the node display in the form of a block by passing arg(1) into the node_load function on line 1 of the previous snippet
Further tweaking your node forms.
If you want to hide certain elements from displaying on your form (for real, not just with CSS), or if you want to change labels or descriptions, you'll need to write a custom module. Don't worry it's very easy. You just need a .info file which holds all your meta data and a .module file which is just a php file.
In your module, use hook_form_alter, then modify any of the array values in the $form object as you please. Setting a field's #access to false will hide it.
mymodule_form_alter(&$form, &$form_state, $form_id){
//Set a message so we can see the whole form object
dsm($form);
//Hide a field called "album" from the node add/edit forms.
$form->field_album['#access'] = false;
//We don't need to save anything because $form is a reference variable!
}
Posted on March 3, 2011 - 10:27pm









Comments
Add new comment