Project:CCK Blocks
Version:6.x-1.1
Component:Code
Category:feature request
Priority:normal
Assigned:Unassigned
Status:active
Issue tags:block, field group

Issue Summary

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;
    }
}
?>

Comments

#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:

<?php
               
if($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?

#3

Status:needs review» postponed (maintainer needs more info)

What's the intention of that code? Why and in which way should we integrate it in cck_blocks?

#4

This 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...

#5

kdebaas
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?

#6

Would 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.

#7

Status:postponed (maintainer needs more info)» active

See #680104: Printing the a Multigroup in a block for some ideas of how to get this working using fieldgroup_groups().

#8

I'd love to see the ability to have a fieldgroup exposed as a block in this module like this.

#9

Title:Expose fieldgroup as block» Expose fieldgroup as block in Drupal-7.x
Version:6.x-1.1» 7.x-1.x-dev

Example in Drupal-7.x

/*
* @file
* Extract field group content to block.
*/

/*
* Implements hook_block_info().
*/
function custom_test_block_info() {
  $groups = field_group_info_groups();
  foreach ($groups as $entity => $bundles) {
    foreach ($bundles as $bundle => $modes) {
      foreach ($modes as $mode => $groups_array) {
        foreach ($groups_array as $group => $group_obj) {
          $delta = $entity . "_" . $bundle . "_" . $mode . "_" . $group;
          $delta32 = crc32($entity . "_" . $bundle . "_" . $mode . "_" . $group);
          $info = $delta;
          $blocks[$delta32] = array(
            'info' => t($info),
            'status' => FALSE,
            'weight' => 0,
          );
        }
      }
    }
  }
 
  return $blocks;
}

/*
* Implements hook_block_configure().
*/
function custom_test_block_configure($delta) {
  $form = array();
  /*
  switch ($delta) {
    case '':
      $form[''] = array(
        '#type' => ,
        '#title' => t(''),
        '#description' => t(''),
        '#default_value' => ,       
      )
    ;
    break;
  }
  */
  return $form;
}

/*
* Implements hook_block_save().
*/
function custom_test_block_save($delta = '', $edit = array()) {
  switch ($delta) {
    case '':
    ;
    break;
  }
 
  return;
}

/*
* Implements hook_block_view().
*/
function custom_test_block_view($delta = '') {
  $block = array();

  // Only support entity 'node' so far
  if (arg(0) == 'node' && is_numeric(arg(1)) && !arg(2)) {
    //dsm("1");
    $node = node_load(arg(1));
    $node_view = node_view($node);
    $groups = field_group_info_groups();
    foreach ($groups as $entity => $bundles) {
      foreach ($bundles as $bundle => $modes) {
        foreach ($modes as $mode => $groups_array) {
          foreach ($groups_array as $group => $group_obj) {
            $delta32 = crc32($entity . "_" . $bundle . "_" . $mode . "_" . $group);
            $content = "";
            //dsm("2");
            // Check delta
            if($delta == $delta32) {
              //dsm("3");
              $block['subject'] = t($group_obj->label);
              foreach ($node_view['#group_children'] as $field => $field_group) {
                // dsm("4");
                if($field_group == $group) {
                  //dsm("5");
                  //dsm($node_view);
                  //dsm($node_view);
                  //show($node_view);
                  // User $node_view renderable array.
                  // If #access = FALSE, it can't be render.
                  $node_view[$field]['#access'] = field_access('view', $field, 'node', $node);
                  $content .= render($node_view[$field]);
                }
              }
              //$block['content'] = !empty($content) ? $content : "No Content";
              $block['content'] = $content;
              // dsm("6");
              // @todo why empty $content?
              //dsm($content);
              // Return and reduce loop.
              return $block;
            }
          }
        }
      }
    }
  }
 
  return $block;
}

#10

Title:Expose fieldgroup as block in Drupal-7.x» Expose fieldgroup as block
Version:7.x-1.x-dev» 6.x-1.1

Reform the version changed, sorry for not familiar with Drupal issue system.

#11

subscribing