Expose fieldgroup as block
eliosh - May 14, 2009 - 20:38
| Project: | CCK Blocks |
| Version: | 6.x-1.1 |
| Component: | Code |
| Category: | feature request |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | needs review |
Jump to:
Description
I'm using this code in one of my production sites.
I think it's useful by itself, but could be more powerful inside this module.
I'm sorry, but following code is not based on coding guidelines.
<?php
function custom_functions_block($op = 'list', $delta = 0, $edit = array()) {
static $mapping = array();
static $groups = array();
if(count($groups)==0){
$groups = fieldgroup_groups('', false, true);
foreach($groups as $nodetype => $nt_groups) {
foreach($nt_groups as $group_name => $group_info){
$crc32 = crc32($nodetype."#".$group_name);
$mapping[$crc32] = array(
'info' => 'CCK FieldGroup: '.($group_info['label'] ? $group_info['label'] : $group_name) . " (".$nodetype.")",
'cache' => BLOCK_NO_CACHE,
'group' => $group_info
);
}
}
}
switch ($op) {
case 'list':
$blocks = array();
if (count($groups)) {
return $mapping;
}
return $blocks;
case 'view':
$block = array();
if (arg(0) == 'node' && is_numeric(arg(1)) && !arg(2)) {
$node = node_load(arg(1));
$group = $mapping[$delta]['group'];
if($group == null){
return $block;
}
$group_name = $group['group_name'];
$output = array();
foreach ($group['fields'] as $field_name => $field) {
$field = content_fields($field_name, $node->type);
$field_view = content_view_field($field, $node);
if (!is_null($field_view)) {
$output[] = $field_view;
}
}
$block['subject'] = $group['label'];
$block['content'] = $output ? implode('', $output) : "Empty";
}
return $block;
}
}
?>
#1
This piece of code is just what I was looking for! I am using it in a custom module.
Thanks
#2
I noticed that in the following code, null is never ever returned:
<?phpif($group == null){
return $block;
}
?>
This has as a consequence that fields that are in a fieldgroup in one content type, and shared (reused) in another content type, but not in a fieldgroup, get displayed anyway in the block next to that other content type.
I adjusted your code to check for the correct content type - group name connection:
<?php
function custom_block($op = 'list', $delta = 0, $edit = array()) {
static $mapping = array();
static $groups = array();
if(count($groups)==0){
$groups = fieldgroup_groups('', false, true);
foreach($groups as $nodetype => $nt_groups) {
foreach($nt_groups as $group_name => $group_info){
$crc32 = crc32($nodetype."#".$group_name);
$mapping[$crc32] = array(
'info' => 'CCK FieldGroup: '.($group_info['label'] ? $group_info['label'] : $group_name) . " (".$nodetype.")",
'cache' => BLOCK_NO_CACHE,
'nodetype' => $nodetype,
'group' => $group_info
);
}
}
}
switch ($op) {
case 'list':
$blocks = array();
if (count($groups)) {
return $mapping;
}
return $blocks;
case 'view':
$block = array();
if (arg(0) == 'node' && is_numeric(arg(1)) && !arg(2)) {
$node = node_load(arg(1));
$nodetype = $node->type;
$group = $mapping[$delta]['group'];
$group_name = $group['group_name'];
$crc32 = crc32($nodetype."#".$group_name);
if($delta == $crc32) {
$output = array();
foreach ($group['fields'] as $field_name => $field) {
$field = content_fields($field_name, $node->type);
$field_view = content_view_field($field, $node);
if (!is_null($field_view)) {
$output[] = $field_view;
}
}
$block['subject'] = $group['label'];
$block['content'] = $output ? implode('', $output) : "Empty";
}
}
return $block;
}
}
?>
What do you think?