Commit 78d3ea24 authored by yched's avatar yched

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

parent b2b934a0
...@@ -9,15 +9,39 @@ ...@@ -9,15 +9,39 @@
/** /**
* Implementation of hook_views_tables(). * Implementation of hook_views_tables().
* *
* Exposes all fields to the views system if the field has delegated its database * Exposes all fields to the views system.
* storage to content.module.
*/ */
function content_views_tables() { function content_views_tables() {
$field_types = _content_field_types(); $field_types = _content_field_types();
$return = array();
$tables = array();
foreach (content_fields() as $field) { foreach (content_fields() as $field) {
$db_info = content_database_info($field); $module = $field_types[$field['type']]['module'];
$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();
$table['name'] = $db_info['table'];
$table['join'] = array(
'left' => array(
'table' => 'node',
'field' => 'vid',
),
'right' => array(
'field' => 'vid',
),
);
$module = $field_types[$field['type']]['module']; $module = $field_types[$field['type']]['module'];
$formatters = array(); $formatters = array();
...@@ -27,73 +51,63 @@ function content_views_tables() { ...@@ -27,73 +51,63 @@ function content_views_tables() {
} }
} }
if (count($db_info['columns'])) { $columns = $db_info['columns'];
$table = array(); $main_column = array_shift($columns);
$addlfields = array();
$table['name'] = $db_info['table']; foreach ($columns as $column => $attributes) {
$table['join'] = array( $addlfields[] = $attributes['column'];
'left' => array( }
'table' => 'node',
'field' => 'vid',
),
'right' => array(
'field' => 'vid',
),
);
$columns = $db_info['columns'];
$main_column = array_shift($columns);
$addlfields = array();
foreach ($columns as $column => $attributes) {
$addlfields[] = $attributes['column'];
}
$table['fields'] = array(); $table['fields'] = array();
$table['fields'][$main_column['column']] = array( $table['fields'][$main_column['column']] = array(
'name' => $field_types[$field['type']]['label'] .': '. $field['widget']['label'] .' ('. $field['field_name'] .')',
'addlfields' => $addlfields,
'sortable' => isset($main_column['sortable']) ? $main_column['sortable'] : FALSE,
'query_handler' => 'content_views_field_query_handler',
'handler' => array('content_views_field_handler_group' => t('Group multiple values'),
'content_views_field_handler_ungroup' => t('Do not group multiple values')),
'option' => array('#type' => 'select', '#options' => $formatters),
'content_db_info' => $db_info,
'content_field' => $field,
'content_field_module' => $module,
);
if (isset($main_column['sortable']) && $main_column['sortable']) {
$table['sorts'] = array();
$table['sorts'][$main_column['column']] = array(
'name' => $field_types[$field['type']]['label'] .': '. $field['widget']['label'] .' ('. $field['field_name'] .')', 'name' => $field_types[$field['type']]['label'] .': '. $field['widget']['label'] .' ('. $field['field_name'] .')',
'addlfields' => $addlfields, 'field' => $main_column['column'],
'sortable' => isset($main_column['sortable']) ? $main_column['sortable'] : FALSE,
'query_handler' => 'content_views_field_query_handler',
'handler' => array('content_views_field_handler_group' => t('Group multiple values'),
'content_views_field_handler_ungroup' => t('Do not group multiple values')),
'option' => array('#type' => 'select', '#options' => $formatters),
'content_db_info' => $db_info, 'content_db_info' => $db_info,
'content_field' => $field, 'content_field' => $field,
'content_field_module' => $module, 'content_field_module' => $module,
); );
if (isset($main_column['sortable']) && $main_column['sortable']) { }
$table['sorts'] = array();
$table['sorts'][$main_column['column']] = array(
'name' => $field_types[$field['type']]['label'] .': '. $field['widget']['label'] .' ('. $field['field_name'] .')',
'field' => $main_column['column'],
);
}
$filters = module_invoke($module, 'field_settings', 'filters', $field); $filters = module_invoke($module, 'field_settings', 'filters', $field);
if (is_array($filters) && count($filters)) { if (is_array($filters) && count($filters)) {
$table['filters'] = array(); $table['filters'] = array();
foreach ($filters as $key => $filter) { foreach ($filters as $key => $filter) {
$filter_name = ''; $filter_name = '';
if (count($filters) > 1) { if (count($filters) > 1) {
$filter_name = (!empty($filter['name'])) ? $filter['name'] : $key; $filter_name = (!empty($filter['name'])) ? $filter['name'] : $key;
$filter_name = ' - '.$filter_name; $filter_name = ' - '.$filter_name;
}
$name = $field_types[$field['type']]['label'] .': '. $field['widget']['label'] . $filter_name .' ('. $field['field_name'] .')';
$init = array(
'name' => $name,
'field' => $main_column['column'],
);
$table['filters'][$main_column['column'] .'_'. $key] = array_merge($filter, $init);
} }
$name = $field_types[$field['type']]['label'] .': '. $field['widget']['label'] . $filter_name .' ('. $field['field_name'] .')';
$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);
} }
// 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;
} }
// 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.
return array('node_data_'. $field['field_name'] => $table);
} }
return $tables;
} }
function content_views_field_query_handler($field, &$fieldinfo, &$query) { 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 ...@@ -129,7 +143,7 @@ function content_views_field_handler_group($field_info, $field_data, $value, $da
} }
$query = "SELECT ".implode(', ', $query_columns). $query = "SELECT ".implode(', ', $query_columns).
" FROM {node} node". " 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. " WHERE node.nid = ".$data->nid.
" ORDER BY node_data_$field[field_name].delta"; " ORDER BY node_data_$field[field_name].delta";
$result = db_query(db_rewrite_sql($query, 'node')); $result = db_query(db_rewrite_sql($query, 'node'));
...@@ -163,27 +177,31 @@ function theme_content_view_multiple_field($items, $field, $data) { ...@@ -163,27 +177,31 @@ function theme_content_view_multiple_field($items, $field, $data) {
/** /**
* Implementation of hook_views_arguments(). * Implementation of hook_views_arguments().
* *
* Exposes all fields as filterable arguments if the field has delegated its database * Exposes all fields as filterable arguments.
* storage to content.module.
*/ */
function content_views_arguments() { function content_views_arguments() {
$field_types = _content_field_types(); $field_types = _content_field_types();
$arguments = array(); $return = array();
foreach (content_fields() as $field) { foreach (content_fields() as $field) {
$db_info = content_database_info($field);
$module = $field_types[$field['type']]['module']; $module = $field_types[$field['type']]['module'];
if (count($db_info['columns'])) { $result = module_invoke($module, 'field_settings', 'arguments', $field);
$main_column = reset($db_info['columns']); if (empty($result)) {
$default_arguments = isset($main_column['default arguments']) ? $main_column['default arguments'] : TRUE; $result = content_views_field_arguments($field);
$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_merge($return, $result);
}
return $return;
}
function content_views_field_arguments($field) {
$db_info = content_database_info($field);
if (count($db_info['columns'])) {
$argument = array();
$argument['name'] = $field_types[$field['type']]['label'] .': '. $field['widget']['label'] .' ('. $field['field_name'] .')';
$argument['handler'] = 'content_views_argument_handler';
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