I have created a content type with 4 field in Drupal 7. One field is of list type, say for listing countries. I have tables in DB for saving country_id and country_name. Is there any way to connect the DB table and content type field? I want to list all these countries in the content type's country field.

  • How you storing country data ? In Custom table or using any entity(node, taxonomy) ? – Rupesh May 13 '14 at 5:46
  • @Rupesh OP said I have tables in DB for saving country_id and country_name. – niksmac May 13 '14 at 5:55

You have to install Content Construction Kit (CCK) module. After installing this module you get a text field for Allowed value function in field setting of desired field. Put the function name here to get country list. Also put following function in a module. You may have to modify query as per your requirement. After doing all these thing you will able to get country list in that field.

enter image description here

function _get_country_list(){
    $country = array(); 
    $query = db_select('country', 'c');
    $query->fields('c', array('country_name','country_id'));
    $result = $query->execute();    
    if ($result) {
        while ($row = $result->fetchObject()) {
            $country[$row->country_id] = t($row->country_name);
    return $country;
| improve this answer | |

You could make use of views.api

Implement views API

function MYMODULE_views_api() {
    return array(
        'api' => '3.0-alpha1', // Youre api version
        'path' => drupal_get_path('module','MYMODULE'). '/views',

and create a file named MYMODULE.views.inc put it in /views folder

Read available functions here views.api.php

| improve this answer | |

If I understood your question right, you was to show a list of countries in your content type field. There is a module for that. Take a look at the countries module.

You can also use the addressfield module if that fits your needs.


you need to create a custom field widget in that case. Take a look at the examples module. Here's a tutorial for the same. Besides, there is a provision in countries module to disable specific countries.

| improve this answer | |
  • OP is not looking for an alternate solution i think. OP is looking for a solution based on what s/he had. – niksmac May 13 '14 at 5:54
  • @Badri Ofcourse, it is a solution. But I want only those countries that already there in my table. – Keerthi May 13 '14 at 6:01

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.