Upgrade Your Drupal Skills

We trained 1,000+ Drupal Developers over the last decade.

See Advanced Courses NAH, I know Enough

Views 1 API - Adding Relationships in PHP

Parent Feed: 

Views 2 has a great feature called Relationships, allowing your view to aggregate fields from various content types which are related some how. For example, say you have a Person content type and a Dependent content type. Within your view, you might want the Person's first/last name, age, etc.. as well as that person's Dependent's first name. With Views 2, you connect these content types in a view via Relationships and all is dandy. In Views 1 (Drupal 5) however, this is impossible without custom code.

I was recently able to pull this off using Views 1 API via hook_query_alter() and appending fields to the $view->field array; however, this isn't the best approach. A better solution would use $query->add_field(), but I couldn't get it working. I'm posting this blog as an interim for someone needing a similar solution, as well as hoping someone might comment with a better solution. Here it is (based on a view with filter:content_type=uprofile):

[my_relationships.module]

<?php/**
* implementation of hook_query_alter
*/
function my_relationships_views_query_alter (&$query, &$view, $summary, $level) {
    if(
$view->name=='manage_discussion_groups'){
       
_uprofile_extra_fields($view);
       
       
// these two important, otherwise the added views filters become statically based on the first page visit.
       
views_invalidate_cache();       
       
$view->is_cacheable = FALSE;
     }
}function
_uprofile_extra_fields(&$view){
   
/* While views_view_add_field should do the trick, it looks like its function parameters are insufficient.
     * So I had to rewrite my own version of that function here:
     * //orig: views_view_add_field($view, $table, $field, $label, $sortable = FALSE, $default_sort = 0, $handler = '');
     */
   
_uprofile_add_field($view, 'bio', 'nid', 'Email', '_go_uprofile_email');
   
_uprofile_add_field($view, 'bio', 'nid', 'Role', '_go_uprofile_role');
}function
_uprofile_add_field(&$view, $table, $field, $label, $handler, $sortable = TRUE, $position = 0, $fullname = '', $id = '', $queryname = ''){
    if(
$position==0) $position = count($view->field) +1;
    if(
$fullname=='') $fullname = "$table.$field";
    if(
$id=='') $id = "$table.$field";
    if(
$queryname=='') $queryname = "$table_$field";
   
   
$view->field[] = array
               (
                  
'tablename' => $table,
                  
'field' => $field,
                  
'label' => $label,
                  
'handler' => $handler,
                  
'sortable' => $sortable,
                  
'position' => $position,
                  
'fullname' => $fullname,
                  
'id' => $id,
                  
'queryname' => $queryname,
               );
   
$view->table_header[] = $label;
   
}function
_uprofile_add_email($fieldinfo, $fielddata, $value, $data) {
    return
db_result(db_query('SELECT u.mail
       FROM {bio} b, {users} u
     WHERE b.uid=u.uid AND b.nid=%d'
, $value));
}function
_uprofile_add_role($fieldinfo, $fielddata, $value, $data) {
    return
db_result(db_query('SELECT r.name
       FROM {bio} b, {users_roles} ur, {role} r
     WHERE r.rid=ur.rid AND ur.uid = b.uid AND b.nid=%d'
, $value));
}
?>
Author: 
RSS Tags: 
Original Post: 

About Drupal Sun

Drupal Sun is an Evolving Web project. It allows you to:

  • Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
  • Facet based on tags, author, or feed
  • Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
  • View the entire article text inline, or in the context of the site where it was created

See the blog post at Evolving Web

Evolving Web