I wanted to create a handler to list the number of rows in a multigroup. This is a working implementation ... but the way I've counted the number of rows in the group is pretty shameful. I couldn't find an API function or see any value in the database which looked like it would give me the information, so I resorted to counting the rows of the first required field in that multigroup.
Here be dragons:
/**
* Implementation of hook_views_handlers().
*/
function content_multigroup_views_handlers() {
return array(
'info' => array(
'path' => drupal_get_path('module', 'content_multigroup'),
),
'handlers' => array(
'content_multigroup_handler_field_multigroup_count' => array(
'parent' => 'views_handler_field_custom',
),
),
);
}
/**
* Implementation of hook_views_data().
*/
function content_multigroup_views_data_alter(&$data) {
// Scan all field groups in the system.
foreach (fieldgroup_groups() as $type_name => $groups) {
$type_label = node_get_types('name', $type_name);
foreach ($groups as $group_name => $group) {
// Let's focus on multigroups that really accept multiple values.
if ($group['group_type'] == 'multigroup' && !empty($group['settings']['multigroup']['multiple'])) {
$db_field = 'multigroup_count_'. $type_name .'_'. $group_name;
$data['node'][$db_field] = array(
'group' => t('Content'),
'title' => t('Multigroup row count @t (@g)', array('@t' => $type_name, '@g' => $group_name)),
'help' => t('Count the number of rows in a multigroup'),
'real field' => NULL,
'field' => array(
'handler' => 'content_multigroup_handler_field_multigroup_count',
'click sortable' => TRUE,
'group_name' => $group_name,
),
);
}
}
}
}
/**
* Views handler
*
* Count the number of rows in a multigroup
*/
class content_multigroup_handler_field_multigroup_count extends views_handler_field_custom {
function render($values) {
// Field a field that exists in this group
$res = db_query('SELECT cgf.field_name
FROM content_group_fields cgf
LEFT JOIN content_node_field cnf
ON cgf.field_name = cnf.field_name
WHERE type_name="%s"
AND group_name="%s"
AND cnf.required = 1',
$values->node_type,
$this->definition['group_name']);
$field_name = db_result($res);
// Count the number of rows for this vid and nid
$res = db_query('SELECT COUNT(nid)
FROM {content_'.$field_name.'}
WHERE vid=%d and nid=%d',
$values->node_vid,
$values->nid);
$count = db_result($res);
return $count;
}
}
Comments
Comment #1
texas-bronius commentedHi Aidan- Did you get anywhere with this old question? Sometimes "shameful" is "meets requirements."
Personally, I'm looking to pick off directly from multigroup fields, modifying output for a specific delta: I don't want to hijack this thread, but thought we might be in a similar camp.
-Bronius
Comment #2
texas-bronius commented(probably your question is more of a support request, as is my hijack)
Comment #3
texas-bronius commentedBut back to your original question, perhaps this is of help?
http://api.drupalecommerce.org/api/cck/cck--content.module/function/cont...