Reputation: 2190
I've been following along with the Views 3 help files for Drupal 7, but I'm a bit stuck on what else I need to add to my module to make it visible to Views so I can use Views to display data in my external database.
Of course my real database has a lot more useful fields, but I was having trouble getting that to display--so I made this test database instead as a "hello world" before I try something more complex. Here's the schema.
database name = other
create table strings (
id int primary key auto_increment,
mystring varchar(50)
);
Here's my settings.php to include the database:
<?php
// ...
$databases = array (
'default' =>
array (
'default' =>
array (
'database' => 'drupal',
'username' => 'drupal_user',
'password' => 'my_other_pass',
'host' => 'localhost',
'port' => '',
'driver' => 'mysql',
'prefix' => '',
),
array (
'database' => 'other',
'username' => 'my_user',
'password' => 'my_pass',
'host' => 'localhost',
'port' => '',
'driver' => 'mysql',
'prefix' => '',
),
),
);
//...
?>
and here's my test.views.inc file for my custom module named test (which is enabled) that describes to Drupal what the table structure of mystrings looks like.
<?php
//useful site explaining all of this: http://groups.drupal.org/node/17236
function test_views_data() {
$data = array(
'strings' => array(
'table' => array(
'group' => t('views test'),
'base' => array(
'field' => 'id',
'title' => t("I guess node"),
'help' => t("help for I guess node I guess"),
'weight' => -10,
'database' => 'others',
),
),
'id' => array(
'title' => t('id'),
/*'field' => array(
'handler' => 'views_handler_field_node',
'click sortable' => TRUE,
),*/
'relationship' => array(
'label' => t("node I think"),
'base' => 'node',
'base_field => 'id',
),
/*'argument' => array(
'handler' => 'view_handler_argument_node_nid',
'name field' => 'id for strings',
'numeric' => TRUE,
'validate type' => 'nid',
),*/
/*'filter' => array(
'handler' => 'views_handler_filter_numeric',
),*/
/*'sort' => array(
'handler' => 'views_handler_sort',
),*/
),
'mystring' => array(
'title' => t('mystring'),
'field' => array(
'handler' => 'views_handler_field',
'click sortable' => TRUE,
),
'filter' => array(
'handler' => 'views_handler_filter_string',
),
'argument' => array(
'handler' => 'views_handler_argument_string',
),
'sort' => array(
'handler' => 'views_handler_sort',
),
),
),
);
return $data;
}
Here's my test.module file:
<?php
function test_help($section) {
switch($section) {
case "admin/help#test":
return "<p>hello from test</p>";
case "admin/modules#description":
return "hello from test inside the admin help thing";
}
}
function test_page() {
return "<p>hello from the actual test page</p>";
}
function test_views_api() {
return array('api' => 3.0);
}
Here's the current problem: When I try and make a new view I don't see any of this information or anything related to my database and table mystrings. Does anybody know what I'm doing wrong in my custom module code / view usage? Any help is much appreciated!
Upvotes: 1
Views: 3164
Reputation: 1535
I just set-up a basic module to test it and it works fine. The main problem I can see with your code is the settings.php file.
settings.php
<?php
$databases = array (
'default' => array (
'default' => array (
'database' => 'drupaldev',
'username' => 'drupal_user',
'password' => '',
'host' => 'localhost',
'port' => '',
'driver' => 'mysql',
'prefix' => '',
),
),
'testdb' => array(
'default' => array (
'database' => 'testdb',
'username' => 'testdb_user',
'password' => '',
'host' => 'localhost',
'port' => '',
'driver' => 'mysql',
'prefix' => '',
),
),
);
so_views.module
<?php
/**
* Implements hook_views_api().
*
* @return array
*/
function so_views_views_api() {
return array(
'api' => 3,
);
}
/**
* Implements hook_views_data().
*
* @return array
*/
function so_views_views_data() {
return array(
'example_table' => array(
'table' => array(
'group' => t('SO View Table'),
'base' => array(
'field' => 'nid',
'title' => t('SO new Table'),
'help' => t('Table contains data'),
'weight' => -10,
'database' => 'testdb',
),
'join' => array(
'node' => array(
'left_field' => 'nid',
'field' => 'nid',
),
),
),
'nid' => array(
'title' => t('Node Id'),
'help' => t('This is the node Id'),
'relationship' => array(
'base' => 'node',
'base field' => 'nid',
'handler' => 'views_handler_relationship',
'label' => t('Default label for the relationship'),
'title' => t('Title shown when adding the relationship'),
'help' => t('More information on this relationship'),
),
),
'plain_text_field' => array(
'title' => t('Plain text field'),
'help' => t('Just a plain text field.'),
'field' => array(
'handler' => 'views_handler_field',
'click sortable' => TRUE, // This is use by the table display plugin.
),
'sort' => array(
'handler' => 'views_handler_sort',
),
'filter' => array(
'handler' => 'views_handler_filter_string',
),
'argument' => array(
'handler' => 'views_handler_argument_string',
),
),
'numeric_field' => array(
'title' => t('Numeric field'),
'help' => t('Just a numeric field.'),
'field' => array(
'handler' => 'views_handler_field_numeric',
'click sortable' => TRUE,
),
'filter' => array(
'handler' => 'views_handler_filter_numeric',
),
'sort' => array(
'handler' => 'views_handler_sort',
),
),
'boolean_field' => array(
'title' => t('Boolean field'),
'help' => t('Just an on/off field.'),
'field' => array(
'handler' => 'views_handler_field_boolean',
'click sortable' => TRUE,
),
'filter' => array(
'handler' => 'views_handler_filter_boolean_operator',
// Note that you can override the field-wide label:
'label' => t('Published'),
// This setting is used by the boolean filter handler, as possible option.
'type' => 'yes-no',
// use boolean_field = 1 instead of boolean_field <> 0 in WHERE statment.
'use equal' => TRUE,
),
'sort' => array(
'handler' => 'views_handler_sort',
),
),
'timestamp_field' => array(
'title' => t('Timestamp field'),
'help' => t('Just a timestamp field.'),
'field' => array(
'handler' => 'views_handler_field_date',
'click sortable' => TRUE,
),
'sort' => array(
'handler' => 'views_handler_sort_date',
),
'filter' => array(
'handler' => 'views_handler_filter_date',
),
),
),
);
}
All the views related code came from the docs. I would recommend installing the advanced_help module and having a look at yoursite.com/views/api-tables.
If you wanted to set this up in a different module then create an info file and the required sql can be found here
Upvotes: 2