I was struggling to get the Groups in an entity to pull and print correctly. I am trying to generate a file with the base information (fields in a group) and wrap each within a group. I am only having trouble pulling the parent groups, not the fields themselves.

<?php
<?php
// Call the used entity in a meta wrapper
$wrapper = entity_metadata_wrapper('entity', $my_entity);

Run an echo command for each field found
foreach ($wrapper->field_collection as $field_name => $value) {
  $field_info = field_info_field($field_name);
  $type = $field_info['type'];
  if ($type == 'datetime') {
    $name = date("m/d/Y", $wrapper->collection->$field_name->value(array('raw')));;
    echo "
<InspecGroup>
  <$type>$name</$type>
</InspecGroup>
";
  }

  else {
    $name = $wrapper->field_collection->$field_name->value(array('raw'));

    echo "
<InspecGroup>
  <$type>$name</$type>
</InspecGroup>
";
  }
}
?>

I'm just not real sure how to wrap this properly with each field group. Tried using a parent for each loop, but was unsuccessful. Anotherwards if we had group A we would end up with the following for each group contained in the entity:

<?php
<GroupA>
  <InspecGroup>
    <$type>$name</$type>
  </InspecGroup>
</GroupA>
?>

Any thoughts?

Comments

JMOmandown’s picture

So I have the following work around, but still struggle to pull to the label value (to name the groups correctly). It would be nice if there is a cleaner and more efficient way then grabbing the information from the DB.

<?php

$group_id = 'group_' . $inspec_id;

$group = db_query("SELECT id FROM {field_group}  WHERE group_name = :group AND bundle = :bundle", array(':group' => $group_id, ':bundle' => $field_collection))->fetchField();


if ($group != $group_stop) {
  echo "
<$group>";
  $group_stop = $group; 
}
?>
nils.destoop’s picture

Issue summary: View changes
Status: Active » Closed (outdated)