Commit 78d3ea24 authored by yched's avatar yched

#117621 - Allow fields to provide their own views integration data

parent b2b934a0
......@@ -9,23 +9,24 @@
/**
* Implementation of hook_views_tables().
*
* Exposes all fields to the views system if the field has delegated its database
* storage to content.module.
* Exposes all fields to the views system.
*/
function content_views_tables() {
$field_types = _content_field_types();
$tables = array();
$return = array();
foreach (content_fields() as $field) {
$db_info = content_database_info($field);
$module = $field_types[$field['type']]['module'];
$formatters = array();
if (is_array($field_types[$field['type']]['formatters'])) {
foreach ($field_types[$field['type']]['formatters'] as $name => $info) {
$formatters[$name] = t($info['label']);
$result = module_invoke($module, 'field_settings', 'tables', $field);
if (empty($result)) {
$result = content_views_field_tables($field);
}
$return = array_merge($return, $result);
}
return $return;
}
function content_views_field_tables($field) {
$db_info = content_database_info($field);
if (count($db_info['columns'])) {
$table = array();
......@@ -41,6 +42,15 @@ function content_views_tables() {
),
);
$module = $field_types[$field['type']]['module'];
$formatters = array();
if (is_array($field_types[$field['type']]['formatters'])) {
foreach ($field_types[$field['type']]['formatters'] as $name => $info) {
$formatters[$name] = t($info['label']);
}
}
$columns = $db_info['columns'];
$main_column = array_shift($columns);
$addlfields = array();
......@@ -66,6 +76,9 @@ function content_views_tables() {
$table['sorts'][$main_column['column']] = array(
'name' => $field_types[$field['type']]['label'] .': '. $field['widget']['label'] .' ('. $field['field_name'] .')',
'field' => $main_column['column'],
'content_db_info' => $db_info,
'content_field' => $field,
'content_field_module' => $module,
);
}
......@@ -82,6 +95,9 @@ function content_views_tables() {
$init = array(
'name' => $name,
'field' => $main_column['column'],
'content_db_info' => $db_info,
'content_field' => $field,
'content_field_module' => $module,
);
$table['filters'][$main_column['column'] .'_'. $key] = array_merge($filter, $init);
}
......@@ -90,10 +106,8 @@ function content_views_tables() {
// We don't use $db_info['table'] for the key, since that may change during
// the lifetime of the field and we don't want to require users to redefine
// their views.
$tables['node_data_'. $field['field_name']] = $table;
return array('node_data_'. $field['field_name'] => $table);
}
}
return $tables;
}
function content_views_field_query_handler($field, &$fieldinfo, &$query) {
......@@ -129,7 +143,7 @@ function content_views_field_handler_group($field_info, $field_data, $value, $da
}
$query = "SELECT ".implode(', ', $query_columns).
" FROM {node} node".
" LEFT JOIN {node_data_$field[field_name]} node_data_$field[field_name] ON node.vid = node_data_$field[field_name].vid".
" LEFT JOIN {". $field_info['content_db_info']['table'] ."} node_data_$field[field_name] ON node.vid = node_data_$field[field_name].vid".
" WHERE node.nid = ".$data->nid.
" ORDER BY node_data_$field[field_name].delta";
$result = db_query(db_rewrite_sql($query, 'node'));
......@@ -163,27 +177,31 @@ function theme_content_view_multiple_field($items, $field, $data) {
/**
* Implementation of hook_views_arguments().
*
* Exposes all fields as filterable arguments if the field has delegated its database
* storage to content.module.
* Exposes all fields as filterable arguments.
*/
function content_views_arguments() {
$field_types = _content_field_types();
$arguments = array();
$return = array();
foreach (content_fields() as $field) {
$db_info = content_database_info($field);
$module = $field_types[$field['type']]['module'];
$result = module_invoke($module, 'field_settings', 'arguments', $field);
if (empty($result)) {
$result = content_views_field_arguments($field);
}
$return = array_merge($return, $result);
}
return $return;
}
function content_views_field_arguments($field) {
$db_info = content_database_info($field);
if (count($db_info['columns'])) {
$main_column = reset($db_info['columns']);
$default_arguments = isset($main_column['default arguments']) ? $main_column['default arguments'] : TRUE;
$argument = array();
$argument['name'] = $field_types[$field['type']]['label'] .': '. $field['widget']['label'] .' ('. $field['field_name'] .')';
$argument['handler'] = 'content_views_argument_handler';
$arguments['content: '. $field['field_name']] = $argument;
}
return array('content: '. $field['field_name'] => $argument);
}
return $arguments;
}
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment