How do I print the multigroup in a block?

Here's how I print the value of one field:

$output = $object->field_name[0]['value'];
print $output;

How do I do a group? Thanks.

Comments

markus_petrux’s picture

Status: Active » Fixed

You can use the function fieldgroup_view_group(). See the doxygen in fieldgroup.module. You can find examples in:

- cck/modules/content_multigroup/panels/content_types/content_multigroup.inc (to render multigroups).
- cck/modules/fieldgroup/panels/content_types/content_fieldgroup.inc (to render standard field groups).

ManyNancy’s picture

Hi thanks, can you please give me a little bit more specific instruction?

I have:

$group = "group_related";
$output = fieldgroup_view_group($group, $node);
print $output;

Which is not working.

I suspect that $group is not supposed to be a string? But looking at the example I still don't understand what format it's supposed to be in.

Thanks.

markus_petrux’s picture

$group is an array. Please, check out examples in the files as described in #1.

ManyNancy’s picture

Status: Fixed » Active

Hi thanks, the example looks like this:

function content_multigroup_content_multigroup_content_type_render($subtype, $conf, $panel_args, $context) {
  if (!isset($context->data)) {
    return;
  }
  $node = drupal_clone($context->data);

  // Extract the node type and fieldgroup name from the subtype.
  list($node_type, $group_name) = explode(':', $subtype, 2);

  // Get a list of all fieldgroups for this node type.
  $groups = fieldgroup_groups($node_type);

  if (!isset($groups[$group_name])) {
    return;
  }
  $group = $groups[$group_name];

  // Render the field group.
  $node->build_mode = NODE_BUILD_NORMAL;
  $group['settings']['display']['label'] = $conf['label'] == 'normal' || !empty($conf['override_title']) ? 'hidden' : $conf['label'];
  $group['settings']['display']['full']['format'] = $conf['format'];
  $group['settings']['display']['full']['exclude'] = 0;
  $group['settings']['multigroup']['subgroup']['full']['format'] = $conf['subgroup'];
  $group['settings']['multigroup']['subgroup']['full']['exclude'] = 0;
  $output = fieldgroup_view_group($group, $node);

  $block = new stdClass();
  if ($conf['label'] == 'normal') {
    $block->title = t($group['label']);
  }
  $block->content = !empty($output) ? $output : $conf['empty'];
  return $block;
}

Looking at this, it puts all the groups into $group? I don't want all the groups, I just want one group.

Thanks.

markus_petrux’s picture

Status: Active » Fixed

You just need this part:

$groups = fieldgroup_groups($node_type);
$group = $groups[$group_name];
ManyNancy’s picture

Status: Fixed » Active

OK, well now I have:

$node_type = "type_name_here";
$groups = fieldgroup_groups($node_type);
$group = $groups["group_name_here"];
$output = fieldgroup_view_group($group, $object);
print $output;

Is it supposed to be like this? This does not work, I don't see any output.

I just need one example, and hopefully I can figure out how to print the rest of my groups. Thanks.

markus_petrux’s picture

The comments in the functions you're using often detail information about the arguments they take.

What do you have in $object? What does the function expect?

ManyNancy’s picture

Status: Fixed » Active

Hi, I don't know what an argument is.

I'm going to copy the function here so I have a reference:

function fieldgroup_view_group($group, &$node, $teaser = FALSE, $page = FALSE) {
  $group_name = $group['group_name'];
  $field_types = _content_field_types();

  // Clone the node to prevent from altering the original.
  $node_copy = drupal_clone($node);

  // Use 'full'/'teaser' if not specified otherwise.
  $node_copy->build_mode = isset($node_copy->build_mode) ? $node_copy->build_mode : NODE_BUILD_NORMAL;

  // Build the content element for individual fields in the field group.
  if (!isset($node_copy->content)) {
    $node_copy->content = array();
  }
  foreach (array_keys($group['fields']) as $field_name) {
    $field = content_fields($field_name, $node_copy->type);

    if (isset($node_copy->{$field_name})) {
      $items = $node_copy->{$field_name};

      // One-field equivalent to _content_field_invoke('sanitize').
      $module = $field_types[$field['type']]['module'];
      $function = $module .'_field';
      if (function_exists($function)) {
        $function('sanitize', $node_copy, $field, $items, $teaser, $page);
        $node_copy->{$field_name} = $items;
      }

      $field_view = content_field('view', $node_copy, $field, $items, $teaser, $page);
      // content_field('view') adds a wrapper to handle variables and 'excluded'
      // fields for node templates. We bypass it and get the actual field.
      $node_copy->content[$field_name] = $field_view[$field_name];
    }
  }

  // Build the content element of the field group itself.
  fieldgroup_build_content($group, $node_copy, $teaser, $page);

  // fieldgroup_build_content() adds a wrapper to handle variables and 'excluded'
  // groups for node templates. We bypass it and render the actual field group.
  $output = drupal_render($node_copy->content[$group_name]['group']);

  return $output;
}

One thing I don't understand is what this means:

$group['group_name'];

It's a format I've never seen before. Does that mean I need to declare $group somewhere or something?

Thanks for all your help! I really appreciate it.

markus_petrux’s picture

Status: Active » Fixed

$object is supposed to contain a $node, yes. Though, I suggest asking in the development forums. Otherwise, you're issue may fall down the queue as many others. This issue is now far beyond CCK matters.

ManyNancy’s picture

I don't understand what you mean by beyond CCK matters, this issue is about displaying a cck fieldgroup? Since $object is $node, please let me know what I need to pass to the function so that it will do the correct things.

Nevermind anything I said about other modules since that's distracting. I'm just trying to render the fieldgroup inside a block. This should not be difficult since so many other modules have done it.

Thanks, and please please help.

stefan81’s picture

Hi, Markus
thanks for your patience and help!

Now I figured it out, Nancy:
Just replace $nodetype and $group_name with your values, then you get the fieldgroup.
In my case I printed the group using its name "group_edition" which is in a nodetype (content type) called "publication"

		<?php
		$nodetype = 'publication';
		$group_name = 'group_edition';

		$groups = fieldgroup_groups($nodetype);
		$group = $groups[$group_name];

		// I would suggest here to explore the contents of $group.
		// You could change formatter options, for example.

		// Create a copy of the node object to make custom changes.
		$node_copy = drupal_clone($node);

		// Now, you need to remove the delta values that you do not need to
		// render a particular subgroup of items from $node_copy.

		$output = fieldgroup_view_group($group, $node_copy);
		print $output;
		?>

It is using the standard "fieldset" display format.
Now I wonder how I can display it as a "simple" styled fieldgroup.

stefan81’s picture

Hi again,
the code above worked inside page.tpl.php.
But not inside a block.
I get an error:

Fatal error: __clone method called on non-object in /mnt/sites/trilogo.de/web/beta02/includes/common.inc on line 1722

any clue what to do?

ManyNancy’s picture

I imagine you need to get the node object, since that's not part of a block? I'm not really sure. I did something like this:

$node_copy = drupal_clone(node_load(array('nid' => arg(1))));

Not that this works either. :( No fatal error but no multigroup displayed either. :(

Thanks for giving fresh insight yanku.

karens’s picture

Status: Active » Fixed

Take a look at http://drupal.org/project/cck_blocks. A couple issues address putting a fieldgroup in a block, like #221182: Viewing CCK Groups as a block and #462924: Expose fieldgroup as block, so it is a feature request there and that is probably the best place for this.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.