Posted by yasheshb on September 19, 2007 at 11:57am
hello.
while playing with cck (drupal 5.2 , cck 5.x-1.5) i realised that there's no way to configure a custom field
to hold unique values. one has to do it programmatically (i could be wrong.. since i've not searched a lot
on the sites). i found this relevant article http://drupal.org/node/100586 (and this one - http://drupal.org/node/89599)
will this feature of having a "unique" property of a custom field be implemented in future releases of cck ?
thx
yashesh bhatia
| Attachment | Size |
|---|---|
| foo.php_.txt | 1.63 KB |
| foo.module.txt | 3.61 KB |

Comments
quick solution..
coded a quick function that works for me.
hope it's useful to others using cck and having a similar need.
<?php
/**
<em>
* This function checks if a value exsists for a cck field
*
* @param string $content_type_name machine readable name of the content type
*
* @param string $field_name machine readable name of field
*
* @param mixed $field_value value that needs to be checked
*
* @return boolean true if present, else false
*
*/
function is_cck_field_value_present($content_type_name, $field_name, $field_value) {
// first lets get field details
$qs =<<<EOD
SELECT *
FROM {node_field}
WHERE field_name = '%s'
EOD;
$result = db_query($qs, "field_{$field_name}");
while($row = db_fetch_object($result)) {
$field_type = $row->type; // we need the type to determine the mysql type
$inline_value = $row->db_storage; // inline values are those which are stored in the content type table
}
// the where param type for mysql comparisions
$where_param_type = "";
switch($field_type) {
case "text":
$where_param_type = "'%s'";
break;
case "number_integer":
$where_param_type = "%d";
break;
case "number_decimal":
$where_param_type = "%f";
break;
}
if ($inline_value) {
$qs =<<<EOD
SELECT count(</em>)
FROM {content_type_{$content_type_name}}
WHERE field_{$field_name}<em>value = {$where_param_type}
EOD;
}
else {
$qs =<<<EOD
SELECT count(*)
FROM {node} n, {content_field</em>{$field_name}} cf
WHERE n.nid = cf.nid AND n.vid = cf.vid AND cf.field_{$field_name}_value = {$where_param_type} AND n.type = '%s'
EOD;
}
$result = db_result(db_query($qs, $field_value, $content_type_name));
return ($result ? true:false);
} // end of function is_cck_field_value_present
?>
yashesh bhatia.
code gets fudged due to <em>
the code posted above got fudged due to em tags added for certain asterix and underscore
this i believe is due to markdown syntax
http://daringfireball.net/projects/markdown/syntax#backslash
i tried escaping the asterix and underscore but somehow am not able to pinpoint
the asterix , underscore that are faulty.
i have 2 suggestions for the owners of this site.
1 - please allow file attachments for posting sample code OR
2 - disable any markups within php tags used for posting code.
it's painful to spend enormous amount of time escaping asterixs and underscores from the code for posting purpose.
i may be doing something wrong here too, so would appreciate any help in how to post code with asterixs and underscores
thanks.
yashesh bhatia.
p.s. in the above code please replace the em tags with appropriate asterix or underscore. or send me an email yasheshb@gmail.com
and i'll send you the file.
pps. the above code will also not work for floats due to mysql problems listed below (not sure of postgres though)
http://dev.mysql.com/doc/refman/5.0/en/problems-with-float.html
http://archives.neohapsis.com/archives/mysql/2005-q4/1241.html
fixed
i rearranged input filters which seems to have fixed your post.
i also enabled uploads for authenticated users.
Where is this function implemented?
Hi yashesh,
I am kind of new to drupal, and am still learning my way around the system. Your function looks interesting and I'd like to play-around with it but I had a question.
Where is the function used?
In a module? As a custom field in your CCK content type? In a theme template file for the form?
I don't see any hooks being used, so I'm not sure when the function is getting triggered.
used in a custom module..
ZrO-1,
I use it in a custom module in the hook nodeapi. i've attached the sample usage file to the original post.
rgd.
yashesh
help...
I've installed the module but i dont know how can i configure it...
I have to edit the .module file with my preferences?
Thnks
unique_field module
Hi yashesh,
Thanks for sharing your approach.
I've released a module called unique_field, which provides an interface for selecting the fields from a content type to require to have unique values. The module is available at:
http://drupal.org/project/unique_field
I'd appreciate any comments and suggestions.
Module unique_field check
Module unique_field check only first value in multiple value types, even if values in nodes have one type
no longer true
Although this thread is very old, thought I'd add that this is no longer true. Given a CCK field that allows multiple values, the module checks the entered value against all the values - of other nodes.
Note that last caveat. I found that if I have a multi-valued text field, I can enter the same value multiple times, and Unique fields won't complain (it will allow the node to be saved).