3

I'm on drupal 7.9 and I'm using the Views 3 rc1. I've successfully created a custom base table that draws data from an external database (a drupal 6 site's database). Here's that code:

<?php
function etlo_mod_views_data() {

//define the content_type_workshop table from the drupal 6 site.
//set it as the base table
$data['content_type_workshop']['table']['group'] = t('Workshops');
$data['content_type_workshop']['table']['base'] = array(
'field' => 'nid',
'title' => t('ETLO Workshops'),
'help' => t("External Etlo Database"),
'weight' => -9,
'database' => 'www2',
);

 //define the node table from the drupal 6 site
$data['workshop_node']['table']['group'] = t('Workshops');

//define the content_field_workshop_category table from drupal 6 site
$data['content_field_workshop_category']['table']['group'] = t('Workshops');


//define the content_field_workshop_grade_level table from drupal 6 site
$data['content_field_workshop_grade_level']['table']['group'] = t('Workshops');


//Link the workshop node table (for getting the title) to the contenty_type_workshop table
$data['workshop_node']['table']['join']['content_type_workshop'] = array(
'table' => 'node',
'left_field' => 'nid',
'field' => 'nid',
);

//Link the workshop category table (for getting the title) to the contenty_type_workshop 
$data['content_field_workshop_category']['table']['join']['content_type_workshop'] =     array(
'left_field' => 'nid',
'field' => 'nid',
);

//Link the workshop grade level table (for getting the title) to the     contenty_type_workshop table
$data['content_field_workshop_grade_level']['table']['join']['content_type_workshop'] = array(
'left_field' => 'nid',
'field' => 'nid',
);



// Define the 'workshop_shortname' field.
$data['content_type_workshop']['field_shortname_value'] = array(
'title' => t('Short Name'),
'help' => t("The course's shortname."),
'field' => array(
'handler' => 'views_handler_field',
'click sortable' => TRUE,
),
'sort' => array(
'handler' => 'views_handler_sort',
),
'filter' => array(
'handler' => 'views_handler_filter_string',
),
);

//define the 'NID' field.  Probably not needed.  Was just testing.
$data['content_type_workshop']['nid'] = array(
'title' => t('NID'),
'help' => t("the workshop nid."),
'field' => array(
'handler' => 'views_handler_field',
'click sortable' => TRUE,
),
'sort' => array(
'handler' => 'views_handler_sort',
),
'filter' => array(
'handler' => 'views_handler_filter_string',
),
);

//define the description  
$data['content_type_workshop']['field_workshop_description_value'] = array(
'title' => t('Workshop Description'),
'help' => t("The course's description."),
'field' => array(
'handler' => 'views_handler_field',
'click sortable' => TRUE,
),
'sort' => array(
'handler' => 'views_handler_sort',
),
'filter' => array(
'handler' => 'views_handler_filter_string',
),
);


//define the title - from the node table
$data['workshop_node']['title'] = array(
'title' => t('Workshop Title'),
'help' => t("The course's full course name."),
'field' => array(
'handler' => 'views_handler_field',
'click sortable' => TRUE,
),
'sort' => array(
'handler' => 'views_handler_sort',
),
'filter' => array(
'handler' => 'views_handler_filter_string',
),
);


//define the grade level - from its own table 
$data['content_field_workshop_grade_level']['field_workshop_grade_level_value'] = array(
'title' => t('Workshop Grade Level'),
'help' => t("The course's grade level."),
'field' => array(
'handler' => 'views_handler_field',
'click sortable' => TRUE,
),
'sort' => array(
'handler' => 'views_handler_sort',
),
'filter' => array(
'handler' => 'views_handler_filter_in_operator',
'options callback' => 'etlo_mod_grade_level_handler_callback',
),
);

//define the category - from its own table 
$data['content_field_workshop_category']['field_workshop_category_value'] = array(
'title' => t('Workshop Category'),
'help' => t("The course's category."),
'field' => array(
'handler' => 'views_handler_field',
'click sortable' => TRUE,
),
'sort' => array(
'handler' => 'views_handler_sort',
),
'filter' => array(
'handler' => 'views_handler_filter_in_operator',
'options callback' => 'etlo_mod_category_handler_callback',
),
);

//define the delta - from the field_workshop_category table 
$data['content_field_workshop_category']['delta'] = array(
'title' => t('Category Delta'),
'help' => t("The entry's delta."),
'field' => array(
'handler' => 'views_handler_field',
'click sortable' => TRUE,
),
'sort' => array(
'handler' => 'views_handler_sort',
),
'filter' => array(
'handler' => 'views_handler_filter_string',

),
);


//define the delta - from the field_workshop_category table 
$data['content_field_workshop_grade_level']['delta'] = array(
'title' => t('Grade Level Delta'),
'help' => t("The entry's delta."),
'field' => array(
'handler' => 'views_handler_field',
'click sortable' => TRUE,
),
'sort' => array(
'handler' => 'views_handler_sort',
),
'filter' => array(
'handler' => 'views_handler_filter_string',
),
);
return $data;
}

Everything there works very well. It allows me to bring in the data from the other database, and I also configured a couple of option callback functions that successfully set values for a couple of filters.

The big problem that I'm having has to do with multiple values in a single field on the drupal 6 site that we're connecting to. On that site, both the 'category' and 'grade_level' fields can have multiple values. When I have this situation, I usually use the "Multiple Field Settings" settings box that shows up when you add a field in the views UI to combine them into a single entry. In my case, that settings panel doesn't show up. I am using the normal 'views_handler_field' handler for these fields...should I be using something else? How can I combine multiple field values into a single views row when the data comes from an external database?

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.