Skip directly to content

CCK

Drupal Quick Tip #1 - Setting CCK Option Selects to Blank as Default and Required

on February 12th, 2008 at 6:38:17 AM

This took me a little bit of poking around to figure out, so here's a quick tip in case others are attempting to do the same thing.

For some CCK select options, you want it to both be required and to also not default on the first answer because a user might click through without changing the default without meaning to. There is the Default value options you can play with, but the easiest way I've found is in your Allowed values list you can simply enter this as your first choice:


|-Please Choose-

This will give you the -Please Choose- as a default, but if the user doesn't switch it, an error will come up. The reason for this is it's expecting you to have your answer in a format such as 1|Option 1, with the "machine" or "key" name first, followed by a pipe, and then the name of the option a user will see. If you don't give it a machine name it simply isn't valid.

Here's an example of an Allowed values list you might have.


|- Choose City-
la|Los Angeles
sd|San Diego
sf|San Francisco
sb|Santa Barbara

Please note that for normal options you don't have to have the key value at all. This isn't the best way to do this, as it now gives you a value without a key which can be troublesome later.

Alternatively, you could use hook_nodeapi. For example, let's say you add in your CCK field


choose|-Choose-

Now, in a custom module, in this case called "siteconfig", you could add:


function siteconfig_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
if ($node->type == 'custom_content_type') {
switch ($op) {
case 'validate':
if ($node->field_city[0]['value'] == 'choose') {
form_set_error('field_city', 'Invalid choice, please choose a city.');
}
break;
}
}
}
?>

Creating "Groups" Using CCK, Views, Node Profile and Node Reference in Drupal

on July 4th, 2007 at 4:55:12 PM

Being an avid user of CCK and Views, I decided to try essentially creating "groups" using them and the CCK node reference. This isn't going to be a step by step how-to, but more of a general proof of concept.

The modules used in order to achieve this functionality:

The idea here is we're going to create a content type to act as the group's main page. For instance, let's say we create a CCK called "group". We'll have the title of the group and a description.

Now we need to set up nodeprofile. For more info on nodeprofile and how it works, check out the documentation. There's also a great step by step tutorial on creating a nice user profile with the nodeprofile set of modules. Basically, it allows for users to be treated as nodes. So, let's say we create a node called "user profile". We'll have fields like name, location, picture, etc. We'll also add a CCK node reference to this node, and and check "group" as a reference. Set it as a nodeprofile.

Now, when a user is created and their nodeprofile is created, you can choose which "group" content type to reference the user to.

There's lots of things that can be done from here, but let's assume we want to create a list of all the users associated with a specific group. We would create a view called "group_user_list", add the correct Node Reference as an argument, filter it by the nodeprofile content type, and add it to the "group" content type. In my situation, I embedded the view directly in node-group.tpl.php


global $current_view;
$current_view->args[0]=$node->nid;
$view1 = views_get_view('group_user_list');
print (views_build_view('embed', $view1, $current_view->args, false, false));
?>

You can also create other content types to associate with the group. A CCK event type, blogs, etc. Creating views to add them onto the main group page is pretty straight forward. Each content type will need the same node reference field, and when creating the view, use the node reference field as the argument. You will need to add:


$args[0] = arg(1);
?>

in the "Arguments Handling Code" section of the view in order to get things to display properly. Voila, groups without using organic groups!