1

I'm trying to get custom fields of my custom content-type "slideshow". I've chosen to get the fields from template (.tpl.php) file for some reasons (mainly because I've to write lots of formatting HTMLs and modifying lots of template files did not sound good).

So far, What I've got is individual nodes:

$result = db_query_range('SELECT n.nid
                      FROM {node} n WHERE n.type = :ntype', 0, 10, array(':ntype' => 'slideshow'));
foreach ($result as $record) {
    $node = node_load($record->nid);
    // Got $node! What to do now????
}

The code works & I've got nodes in $node. Now What to do to get individual custom fields & work with the raw data of these fields? Please help.

PS I've tried Get content type with PHP from database but the code does not work... guess it's for older Drupal versions.

2

You could always try field_get_items if you know the names of the fields you want. field_get_items('node', $node, 'field_name');

Although node_load() should be providing the fields for you and can be found at $node->field_name

Also Attiks advice is sound, I would also not recommend doing this in tpl

1

First a bit of advise, take it or leave it: but don't do this in the tpl.php files, try to use some preprocessing hooks.

To see what fields are available on your $node, add a print_r($node) after the node_load, it will show you everything that's defined on the $node.

If you want total control over your display without writing lots of template files have a look at Display suite and custom_formatters

1
  • Thanks - I almost forgot about doing this within preprocessor! – Shafiul Oct 20 '11 at 3:18
1

Attiks is right! first use print_r($node), or var_dump($node). if you view the source of the page where the info is printed it will probably display in a readable form.

Use the following format or similar (depending on what you need) to get your data:

$node->field_my_cck_field['und'][0]['value']
3
  • Thanks. What does 'und' stand for? I've seen it in many places. – Shafiul Oct 20 '11 at 3:17
  • Language neutral uses the language code 'und', which means undefined and is specified to be used for such cases by the ISO 639 standard. You'll see this language code in Drupal source code all around, so good to know. (see hojtsy.hu/blog/2011-jan-31/…) – Attiks Oct 20 '11 at 7:19
  • it stands for language (undefined in this case...) – Nevos May 30 '12 at 7:56
1

To get a list of fields for a node content type in Drupal 7, try the field_info_instances function. Here is an example usage that will retrieve all the fields for a custom node content type:

$my_fields = field_info_instances("node", "my_custom_content_type");
0

Use this function to get an array of content type with its fields

 function get_content_fields_array() {

$list = node_type_get_names(); //get the list of content types
$contenttypes = array();
$type_count =0;
foreach ($list as $key => $value) {
    $field_list = field_info_instances('node', $key); //get field list for content type
    $field_count =0;
    $field_max = count($field_list);
    foreach ($field_list as $field_key => $field_value) {
        if($field_count == 0) {
            $contenttypes [$type_count]['label'] = $value;
            $contenttypes [$type_count]['machine_name'] = $key;
        }
        $contenttypes [$type_count]['fields'][$file_field_count]['machine_name'] = $field_key;
        $contenttypes [$type_count]['fields'][$file_field_count]['field'] =  $field_value;

        if($field_count == ($field_max -1)) {
                $type_count ++;
        }
        $field_count++;
    }
}

 return $contenttypes;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.