tags:

views:

55

answers:

4

I have a table that's been created by a module. I need to include some of its fields into an existing view.

I tried using the table wizard module, but all it does is create a separate view for that table. I'd like to be able to choose fields from that table to be added into an existing view as additional fields, or through relationships or something like that. Is there a workaround to do what I'm trying to do?

+1  A: 

You can use hook_views_data to define your table in code. As long as you don't want views to do special manipulations, it's almost as simple as defining the table with the schema API.

googletorp
+2  A: 

Ah. Views. Took me a while as well. This answer is for Drupal 6 and in the abstract shows how to define fields as well as using a relationship to allow the fields to link to the node table.

Inside modulename.module, you want a function that goes:

function modulename_views_api() {
  return array(
    'api' => 2,
  );
}

Then you want to make a file called modulename.views.inc and define a function like this:

function modulename_views_data() {
    $data['modulename_table'] = array(
        'table'     => array(
            'group'     => 'ModuleName',
            'title'     => 'Module name title',
        ),
        'join'  =>  array(
            // to join to node, we'll use a field in modulename_table called 'nid'
            'node'      => array(
                'left_field'    =>  'nid',
                'field'         =>  'nid',
            ),
        ),
    );

    // now we define the fields in the table like this
    // check out modules/views/handlers to see more specific handlers

    $data['modulename_table']['fieldname'] = array(
        'title'     => 'fieldname',
        'help'      => 'fieldname description',
        'field'    => array(
            'handler' => 'views_handler_field',
        ),
    );

    $data['modulename_table']['nid'] = array(
        'title'     => 'related node',
        'help'      => 'the field that relates back to {node}',
        // here we implement a relationship to nid
        'relationship'  => array(
            'base'      => 'node',
            'field'     => 'nid',
            'handler'   => 'views_handler_relationship',
            'label'     => 'modulename row node',
        ),
        // this relationship can be turned on in views
    );

    return $data;
}
kurreltheraven
+1  A: 

Your other option is to use table wizard to expose the tables to the database and then use the migrate module to create the views. http://drupal.org/project/migrate

Mixologic
I thought about migrate, but this is a live table. So wouldn't migrate transfer only existing data? What about the new data that gets added to the table after the migration.
Berming
+1  A: 

I have found that the Views Custom Field module lets me do just about anything I need as far as adding oddball fields to views .. maybe it'd help ..

Scott Evernden