note- this is taken from KarenS's post to the development list

To access data stored by CCK within another module, you need to determine which table it's stored in.

To find the right table for a field for a SQL query, use the CCK API, like this:

$db_info = content_database_info($field);
$table = $db_info['table'];

That will always return the right table for the requested field.

$field needs to be the complete field array, not just the field name, so if you only have the field name, you would do:

$field = content_fields(my_field_name);
$db_info = content_database_info($field);
$table = $db_info['table'];

content_database_info($field) also contains info about the columns declared by the field so you know what field names to use in a SQL query. It returns an array that looks like:

Array (
    [table] => content_type_story
    [columns] => Array (
            [value] => Array  (
                    [type] => varchar
                    [length] => 50
                    [not null] => 1
                    [default] => ''
                    [sortable] => 1
                    [column] => field_phone_value
                )
            [value2] => Array (
                    [type] => int
                    [length] => 10
                    [unsigned] => 1
                    [not null] => 1
                    [default] => 0
                    [column] => field_phone_type
                )
        )
)

Comments

rfay’s picture

In many cases, we know the table where something is stored, so don't need to do the procedure outlined here.

Is the best way to access a known field in a known content_type table just to do the database call:

select c.* from {content_type_xxx} c inner join node n on c.nid=n.nid and c.vid=n.vid

??? or is there a better programmatic way to do it?

isaac77’s picture

even if you know what table is _currently_ used for a particular field, that could change if the field is added to (or removed from) other content types. the code outlined above by KarenS ensures always getting the correct, current table. sorry if you are already aware of that, but thought the clarification might help others.

Kpolymorphic’s picture

If you use this code for populating cck field default values, take care to rename the $field, $table, and $db_info variables to something unique; because, the field default value code will be running in the same variable scope as the cck core modules (and the cck core is using these variable names). You can get some crazy errors when your default value code overwrites the core variables during validation.

a6hiji7’s picture

Thanks, that's very helpful. I am making a module for custom user search and need to match field values in a Content Profile content and I found this topic helpful to allow the user to use any of the Content Profile types that she may create.

khaos119’s picture

Thanks!! I was searching for an hour on this!!! :/

glenn_echo’s picture

This info was hard to find... Thank You! I've been looking for how to collect field information so I can use it as part of a custom form. This is a good start. Where can I find a function that provides a list of fields for a given content type? Why isn't there better documentation for the CCK api?

glenn_echo’s picture

I figured out that a call to content_fields without passing any values returns a list of all content_fields. That array has each field type and the content type that uses that field type.

$field = content_fields();
dprint_r($fields);

Array
(
[field_media_type] => Array
(
[field_name] => field_media_type
[type_name] => campaign
...
);

mmatsoo’s picture

I had hard-coded a db query and when I was adjusting my code to be CCK-friendly (by using content_fields() and content_database_info()) I spent some frustrating minutes wondering why a print_r of $field was coming up empty.

<?php
$field = content_fields('field_team_nid'); // field_team_nid was field name in table so I figured it should work here.
print_r($field); // This was coming up empty.
?>

My hard-coded query worked fine using "...WHERE field_team_nid = %d" because that's the field's actual name in the table. However, it turns out that I didn't need the "_nid" at the end of the field name.

This code got me what I wanted.

<?php
$field = content_fields('field_team'); // "field_team" works!
print_r($field); // This returned the desired array info.
?>
charlysole’s picture

Hi everyone!
Im trying to use a content profile field filled by users for build a new field select (allowed values) list and almost works

My array looks like this

    [field_empleados] => Array
        (
            [0] => Array
                (
                    [value] => User One
                )

            [1] => Array
                (
                    [value] => User Two
                )

            [2] => Array
                (
                    [value] => User Three
                )

            [3] => Array
                (
                    [value] => User four
                )

        )

I have tried this way:

global $user;
$user_profile = content_profile_load('profile', $user->uid);
$valor = $user_profile->field_empleados;
$array = array($valor);
foreach ($array as $nombre);
return ($nombre);

When i paste this code into my select list field It returns me the value list this way:

< None>
0
User One
1
User Two
2
User Three
3
User Four

They are grouped...
Any ideas of how can i "ungroup" those values from the list?

The desired result is:

< None>
User One
User Two
User Three
User Four

Thanks!