I wrote a module to interface with Views 3 using Drupal 7, but when I create a view using my custom table as a data source, no data shows up. Here's my schema from MySQL:
+-------------+------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| title | mediumtext | NO | | NULL | |
| Department | text | NO | | NULL | |
| credits | int(10) | NO | | NULL | |
| description | longtext | NO | | NULL | |
+-------------+------------+------+-----+---------+----------------+
And here's the hook override in my_module.views.inc:
function my_module_views_data() {
$tableName = 'My_Awesome_Table';
$data = array();
$data[$tableName]['table']['group'] = t('Courses');
$data[$tableName]['table']['base'] = array(
'field' => 'id',
'title' => t('Courses'),
'help' => t("Contains courses, departments, and descriptions.")
);
$data[$tableName]['title'] = array(
'title' => t('Course name'),
'help' => t('Course name'),
'field' => array(
'handler' => 'views_handler_field',
'click sortable' => TRUE,
),
'sort' => array(
'handler' => 'views_handler_sort',
),
'filter' => array(
'handler' => 'views_handler_filter_string',
),
'argument' => array(
'handler' => 'views_handler_argument_string',
),
);
$data[$tableName]['Department'] = array(
'title' => t('Course department'),
'help' => t('Course department'),
'field' => array(
'handler' => 'views_handler_field',
'click sortable' => TRUE,
),
'sort' => array(
'handler' => 'views_handler_sort',
),
'filter' => array(
'handler' => 'views_handler_filter_string',
),
'argument' => array(
'handler' => 'views_handler_argument_string',
),
);
$data[$tableName]['credits'] = array(
'title' => t('Credits'),
'help' => t('Number of credit hours'),
'field' => array(
'handler' => 'views_handler_field',
'click sortable' => TRUE,
),
'argument' => array('handler' => 'views_handler_argument_numeric'),
'filter' => array('handler' => 'views_handler_filter_numeric'),
'sort' => array('handler' => 'views_handler_sort_numeric')
);
$data[$tableName]['description'] = array(
'title' => t('Course description'),
'help' => t('Course description'),
'field' => array(
'handler' => 'views_handler_field',
'click sortable' => TRUE,
),
'sort' => array(
'handler' => 'views_handler_sort',
),
'filter' => array(
'handler' => 'views_handler_filter_string',
),
'argument' => array(
'handler' => 'views_handler_argument_string',
),
);
$data[$tableName]['id'] = array(
'title' => t('Unique identifier'),
'help' => t('Primary key for table'),
'field' => array('handler' => 'views_handler_field'),
'argument' => array('handler' => 'views_handler_argument_numeric'),
'filter' => array('handler' => 'views_handler_filter_numeric'),
'sort' => array('handler' => 'views_handler_sort_numeric'));
return $data;
}
Does something look wrong with my mapping? When I create a View, I'm trying a simple Unformatted List and just displaying each field with a label. The only field that shows up with data is id
, and all of the id
values from my table are there. I tried adding a Filter to the View so that Course Department != blank and Course Name != blank , which didn't eliminate any results (based on the id
s that are shown). Here's the query Views is generating:
SELECT My_Awesome_Table.title AS My_Awesome_Table_title,
My_Awesome_Table.Department AS My_Awesome_Table_Department,
My_Awesome_Table.description AS My_Awesome_Table_description,
My_Awesome_Table.credits AS My_Awesome_Table_credits,
My_Awesome_Table.id AS id
FROM {My_Awesome_Table} My_Awesome_Table
WHERE (( (My_Awesome_Table.Department NOT LIKE '' ESCAPE '\\') AND
(My_Awesome_Table.title NOT LIKE '' ESCAPE '\\') ))
When I run this in phpMyAdmin, just removing the {
and }
from around My_Awesome_Table
, it returns results and there is data in each column.
Edit: it's perhaps relevant to say that I made another custom View module for a different table in the same database, and it works fine. I used that module as a base for this one, changing the module name, function prefixes, etc.
Source: Tips4all
Clear cache, disable module 'my_module', save, then enable it back, save
ReplyDeleteIn my development, I have to add this hook to make it works
function my_module_views_api() {
return array('api'=>2.0);
}