I need to print the contents of a node containing a multigroup. My multigroup contains the following fields. Currently, I have this in the email:

Course 1
Name: <?php print $node->field_courses[0][value]; ?>
Number: <?php print $node->field_coursenumber[0][value]; ?>
College: <?php print $node->field_collegecourse[0][value]; ?>
Credits: <?php print $node->field_gradcredits[0][value]; ?>
Duration: From: <?php print $node->field_cduration[0][value]; ?> To: <?php print $node->field_cduration[0][value2]; ?>

This works fine, but how do I show any other item created by the user? For example, I posted Course 1, but what if the user creates a Course 2 containing all the same fields?

Comments

markus_petrux’s picture

You should iterate through delta values as if the fields where defined with multiple values without multigroup. ie. [0] is just the first value, but there may be more, so you should do a loop.

markus_petrux’s picture

Status: Active » Fixed

Assuming the support request is resolved. Please, re-open if there's still anything else that needs clarification. Thanks

ballerjones’s picture

Status: Fixed » Postponed (maintainer needs more info)

As much as I appreciate your response, I am very much a PHP newb, so I learned very little. I have heard of delta and loop, but I have no idea what they are or how to use them. I will do some research, but if you could post some code leading me in the right direction, that would be great.

markus_petrux’s picture

Status: Postponed (maintainer needs more info) » Fixed

Well, there is always more than one way to do it, but maybe it could be something like this:

// Compute number of items in the multigroup based on just one field.
$items_count = count($node->field_courses);
foreach ($i = 0; $i < $items_count; $i++) {
  print 'Course '. ($i + 1) .'<br/>';
  print 'Name: '. $node->field_courses[$i]['value'] .'<br/>';
  print 'Number: '. $node->field_coursenumber[$i]['value'] .'<br/>';
  print 'College: '. $node->field_collegecourse[$i]['value'] .'<br/>';
  print 'Credits: '. $node->field_gradcredits[$i]['value'] .'<br/>';
  print 'Duration: From: '. $node->field_cduration[$i]['value'] .' To: '. $node->field_cduration[$i]['value2'] .'<br/>';
}

If you need further assitance, then I would suggest asking in the forums.

ballerjones’s picture

Unfortunately, the code you posted doesn't play nice with email sent by Rules. It fails to send, indicating the PHP could not parse. My suspicion is that your code is too complex, but only because the task is too complex. I may need to find a new workaround entirely.

markus_petrux’s picture

Here's something you can do to view the structure of the node. Enter the following in the message body option of the rule:

var_export($node);

Yes, my code in #4 is not correct. I was using foreach incorrectly. Here's probably a better version:

<?php $course_index = 1; ?>
<?php foreach (array_keys($node->field_courses) as $delta) : ?>
Course <?php $course_index++; ?>
Name: <?php print $node->field_courses[$delta][value]; ?>
Number: <?php print $node->field_coursenumber[$delta][value]; ?>
College: <?php print $node->field_collegecourse[$delta][value]; ?>
Credits: <?php print $node->field_gradcredits[$delta][value]; ?>
Duration: From: <?php print $node->field_cduration[$delta][value]; ?> To: <?php print $node->field_cduration[$delta][value2]; ?>
<?php endforeach; ?>

I haven't tested this, so there may be errors. I'm sorry, but I don't know how to help you debug PHP code executed in rules.

Status: Fixed » Closed (fixed)

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

jgarbe’s picture

Thanks for this code, markus. I was using the multi-group in conjunction with the Content Profile node-type for a community, and it wasn't automatically outputting it (not surprising). Here's my version--the same syntactically, just resulting in different output:

<p class="profile-title">Presentations and Publications</p>
<?php $pres_index = 1; ?>
<?php foreach (array_keys($node->field_pres_title) as $delta) : ?>
<?php print ("<i>".$node->field_pres_title[$delta][value].",</i> ".$node->field_pub_event[$delta][value].". ".$node->field_pub_date[$delta][value]."."); ?>
<?php endforeach; ?>

results in:

<p class="profile-title">Presentations and Publications</p>
<i>If You Must Litigate, Litigate Often,</i>All-Lawyer Convocation. March 31, 1983.

Hopefully this will help other chowderheads like me. Great module!