With the excellent views advanced help and this issue, I've managed to create a working view using hook_views_data(). However I still need to create a custom handler something like views_handler_field_one2many. Perhaps this issue could become a mini howto.
A simplified version of what I have is: Each row in tour_booking has one or more matching rows in tour_booking_group.
mysql> desc tour_booking;
+---------------------------+----------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------------------+----------------------+------+-----+---------+----------------+
| bkid | int(10) unsigned | NO | PRI | NULL | auto_increment |
| date_created | int(10) unsigned | NO | MUL | 0 | |
mysql> desc tour_booking_group;
+-----------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+------------------+------+-----+---------+-------+
| bkid | int(10) unsigned | NO | PRI | 0 | |
| delta | int(10) unsigned | NO | PRI | 0 | |
| name1 | varchar(255) | NO | | NULL | |
Here is the code of the hook:
function tour_booking_views_data() {
$data = array();
$data['tour_booking'] = array(
'table' => array(
'group' => t("Tour booking"),
'base' => array(
'field' => 'bkid',
'title' => t("Tour booking"),
'help' => t("Tour's base booking table"),
//'weight' => 0,
),
),
'bkid' => array (
'title' => t("BKID"),
'help' => t("Booking id: primary key"),
'field' => array(
'handler' => 'views_handler_field_numeric',
'click sortable' => TRUE,
),
'filter' => array(
'handler' => 'views_handler_filter_numeric',
),
'sort' => array(
'handler' => 'views_handler_sort',
),
),
'date_created' => array (
'title' => t("Date created"),
'help' => t("When the booking was placed"),
'field' => array(
'handler' => 'views_handler_field_date',
'click sortable' => TRUE,
),
'sort' => array(
'handler' => 'views_handler_sort_date',
),
'filter' => array(
'handler' => 'views_handler_filter_date',
),
),
//plus many more field definitions
);
$data['tour_booking_group'] = array(
'table' => array(
'group' => t("Tour booking"),
'join' => array(
'tour_booking' => array(
'left_field' => 'bkid',
'field' => 'bkid',
),
),
),
'bkid' => array (
'title' => t("BKID"),
'help' => t("1st part of groups primary key"),
),
'delta' => array (
'title' => t("Group delta"),
'help' => t("2nd part of groups primary key"),
'field' => array(
'handler' => 'views_handler_field_numeric', // *** here ***
),
'sort' => array(
'handler' => 'views_handler_sort',
),
),
'name1' => array (
'title' => t("Group name 1"),
'help' => t("Group member's first name"),
'field' => array(
'handler' => 'views_handler_field', // *** and here ***
),
),
//plus many more field definitions
);
return $data;
}
At present I get a complete row of all fields for each of the rows in the second table tour_booking_group.
What I would like is all of the rows from tour_booking_group that match a row in tour_booking, to be part of that row.
E.G.:
BKID: 1
Date created: 19.01.2010
Delta: 0, 1, 2, 3
Group name 1: John, Mary, Hans, Eva
BKID: 2
etc.
This is possible with CCK when using fields with multiple values. My guess is the CCK custom handler for this is content_handler_field_multiple. It's beyond my knowledge to fathom how it works.
My guess is I will need to create a class that extends views_handler_field using handler->pre_query() to pre-fetch the sub-rows from tour_booking_group.
I would very much appreciate any help and pointers anybody could offer.
Thanks Richard
Comments
Comment #1
bird-cage commentedI've closed this due to an obvious lack of interest.
Comment #2
m_z commented@bird-cage:
Please take a look at views_handler_field_term_node_tid.inc field handler (located at views/modules/taxonomy) where all taxonomy terms related to a given node ID are put together in a individual field.
This is realized by doing a sub-query in pre_render function.
You can take this example together with taxonomy.views.inc (located at views/modules) to build 2 own field handlers: Replace "vid" with your connecting key "bkid" and in the 1st handler you select "delta"s (and clue it togehter via ",") and in the 2nd handler you do the same for "name1".
Good luck!