Hi All,
I have been struggling with Views API for the last 2 days and have decided to ask the community for some help.
Background: I have 3 custom tables in my database that are filled with data from CSV files from an external source, I am attempting to expose some columns of these tables as fields to Views so that I can display them on the website. These tables have no relationship to nodes, however I have a linkage table that relates the users table (uid) to the primary key of one of the tables - bringing that relationship into views will be needed later on when I overcome the hurdle of just getting fields to display.
The install schema for the table I'm trying to use:
<?php
$schema['mytable'] = array(
'description' => t(''),
'fields' => array(
'cust_id' => array(
'description' => t(''),
'type' => 'serial',
'length' => '11'
),
'cust_num' => array(
'description' => t(''),
'type' => 'int',
'length' => '11'
),
'cust_name' => array(
'description' => t(''),
'type' => 'varchar',
'default' => 'NULL',
'length' => '255'
),
),
'primary key' => array('cust_id'),
);
?>What I have coded up at the moment (I have tried lots of other things too, although this is probably the cleanest way to show my problem)
mymodule.module
<?php
function mymodule_views_api() {
return array(
'api' => '2.0',
);
}
?>mymodule.views.inc (placed in the same directory as mymodule.module)
<?php
function mymodule_views_data() {
$data = array();
$data['mytable'] = array(
'table' => array(
'group' => t('MyTable'),
'title' => t('My Table'),
'base' => array(
'field' => 'cust_id',
'title' => t('Customer ID'),
'help' => t('Customer ID'),
),
),
'cust_name' => array(
'group' => t('MyTable'),
'title' => t('Customer Name'),
'help' => t('Name of Customer'),
'field' => array(
'handler' => 'views_handler_field',
'click sortable' => TRUE,
),
),
);
return $data;
}
?>From my understanding of the Views API, that should give me a field named 'Customer Name' under the category 'MyTable' in the views UI. - But it does not appear.
Am I missing some fundamental piece of information here? Am I supposed to join this table to something so Drupal can identify which row to pull from the specified column? Kinda scratching my head as to where to go with it all
Comments
Ok so I figured it out,
Ok so I figured it out, typically it was right after I posted for help..
The problem was that I was using a view I had already created configured to show 'Content' (The 'Show' dropdown in the Add new view dialog). I went back and created a new view, this time selecting 'Customer ID' from the dropdown and now my fields are all there.
<?php//this is where the dropdown was populated from
'base' => array(
'field' => 'cust_id',
'title' => t('Customer ID'),
'help' => t('Customer ID'),
),
?>
Either I missed it or the Views docs isnt really clear on it, but defining a 'base' table in hook_views_data() places a new item in the 'Show' dropdown on the Add new view dialog, and to use your tables (that are unrelated to nodes etc) you need to use that.
_
Another option to consider is defining your custom tables as entities-- then you get views (and other entity aware modules) functionality for 'free'.
_
Don't be a Help Vampire - read and abide the forum guidelines.
If you find my assistance useful, please pay it forward to your fellow drupalers.