I'm working on migrating a site using tw/migrate and need a multi-column join. I could munge the source data to work around the need, but though it would be nice to have the feature in tw. The source data has two tables, document and document_version, which is nearly identical to Drupal's node and node_revisions except that the version column is not unique like vid. The primary key is (document_id, version_num), hence the need.
I can't figure out an easy way to change the tw relationships UI to allow adding multi-column joins. Also, this isn't likely to be a (very) common need, so why don't we just add something like a hook_tw_relationship_alter(&$table_left, &$table_right, $tw_relationship)? This would be at the end of the $row loop in function _tw_generate_views_relationship_data(). I already have a module with hooks implemented for migrate, so it's then trivial to implement this hook and add my 'extra' and 'extra type' join elements for views.
Invoking the hook for individual relationships makes sense to me (I think), but this is specifically from the mindset of adding additional join fields per relationship setup in the UI. Alternatively we could just throw a drupal_alter('tw_views_data', $tables) at the end of tw_views_data() and it could be used for whatever. One drawback to both of these approaches is none of this alteration is visible in the UI. Perhaps there should be three tw relationship types, Automatic, Manual, and Hook? Just a thought.
Attached is a patch that adds hook_tw_relationship_alter() (which I'd document if accepted), and here's a sample implementation while adds a version field to the existing manual join.
function example_tw_relationship_alter(&$table_left, &$table_right, $tw_relationship) {
extract($tw_relationship);
$rawtbl1 = tw_qualified_tablename($dbcon1, schema_unprefix_table($tbl1));
$rawtbl2 = tw_qualified_tablename($dbcon2, schema_unprefix_table($tbl2));
if ($rawtbl1 == 'document' && $col1 == 'document_id'
&& $rawtbl2 == 'document_version' && $col2 == 'document_id') {
$table_right['table']['join'][$rawtbl1]['extra'] = 'document.active_version = document_version.version';
$table_right['table']['join'][$rawtbl1]['extra type'] = 'AND';
}
}
| Comment | File | Size | Author |
|---|---|---|---|
| #1 | tw_views_data_alter.patch | 432 bytes | cedarm |
| tw_relationship_alter.patch | 657 bytes | cedarm |
Comments
Comment #1
cedarm commentedNow having implemented both hook_tw_relationship_alter() and hook_tw_views_data_alter() I'm leaning towards the latter. It's just simpler overall.
Patch for hook_tw_views_data_alter() attached. Sample implementation (standalone; requires no Manual or Automatic tw relationship at all):
For completeness perhaps a companion hook_tw_views_data_alter_describe() that would list relationships on admin/content/tw/relationships? Or is that just overkill?
Comment #2
eileenmcnaughton commentedsubscribing
Comment #3
mikeryanNice enhancement, thanks!
Comment #4
cedarm commentedWhich one? Need to take the other out of my code.
Comment #5
mikeryanhook_tw_views_data_alter()
Comment #7
eileenmcnaughton commentedhook_tw_views_data_alter() is the one I'm using - & re-visiting this thread reminds me whey I can't upgrade tw - because I'd forgotten I had patched it !