Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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.
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;
}
}
Comments
Comment #1
kdebaas CreditAttribution: kdebaas commentedThis piece of code is just what I was looking for! I am using it in a custom module.
Thanks
Comment #2
kdebaas CreditAttribution: kdebaas commentedI noticed that in the following code, null is never ever returned:
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:
What do you think?
Comment #3
Anonymous (not verified) CreditAttribution: Anonymous commentedWhat's the intention of that code? Why and in which way should we integrate it in cck_blocks?
Comment #4
Dubs CreditAttribution: Dubs commentedThis is a really great piece of code. It performs the same task as CCK blocks, but allows CCK Fieldgroups to also be exposed. Thanks for the code...
Comment #5
alb CreditAttribution: alb commentedkdebaas
seem work;
is possible to have in the blocks, the list of names(labels) of the fields also if there aren't the values?
example for company's datas may seem you read all the datas and it is not clear however that other information were not included
but see that there isn't a botton for to save and create the blocks, when they are generates after the first time?
every time one save the general modules page or when in the content type is insert a cck or is channged a group?
Comment #6
michaellander CreditAttribution: michaellander commentedWould love to see this work with the http://drupal.org/project/field_group module in drupal 7. I'll create a separate issue if you would like.
Comment #7
KarenS CreditAttribution: KarenS commentedSee #680104: Printing the a Multigroup in a block for some ideas of how to get this working using fieldgroup_groups().
Comment #8
Melissamcewen CreditAttribution: Melissamcewen commentedI'd love to see the ability to have a fieldgroup exposed as a block in this module like this.
Comment #9
kolier CreditAttribution: kolier commentedExample in Drupal-7.x
Comment #10
kolier CreditAttribution: kolier commentedReform the version changed, sorry for not familiar with Drupal issue system.
Comment #11
castawaybcn CreditAttribution: castawaybcn commentedsubscribing