Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I have set the 'Body field settings' > 'Number of values' to 'Unlimited'; so the user can add as many 'body' fields as they like.

I have then added the following piece of code to my themes 'node.tpl.php' file:

<?php $i=0; 
while ($i<=10) : ?> 
<section class="box page-box"> 
<?php print render($content['body'][$i]); 
$i++; ?>
</section>
<?php endwhile; ?> 

This code outputs each of the 'body' fields in a section/div and then adds a number of blank sections/divs until it hits 10!

How would I find out the total number of 'body' fields so that it only outputs the available number and not all 10? I was trying to use 'foreach' but couldn't seem to get it working.

share|improve this question
    
foreach is the appropriate control structure to use in this instance. You should post the code that you used when attempting to use foreach and explain what the unintended result was, so that you can get help with executing the appropriate solution, rather than trying to hack around the real issue. –  sheena_d May 18 '12 at 3:36
add comment

2 Answers

up vote 1 down vote accepted

This issue was solved using a function in my template.php file to get the field data out of entities, which I found here.

/**
 * Returns field values as actual entities where possible,
 * also allows selection of individual items to be returned
 */
function field_fetch_field_values($entity_type, $entity, $field_name, $get_delta = NULL, $get_key = NULL) {
  $values = array();
  if (isset($entity->$field_name) && !empty($entity->$field_name)) {
    foreach (field_get_items($entity_type, $entity, $field_name) as $delta => $item) {
      $value = $item;
      $keys = array_keys($item);
      if (count($keys)==1) {
        $key = $keys[0];
        switch ($key) {
          case 'nid':
            $value = array_shift(entity_load('node', array($item[$key])));
            break;
          case 'uid':
            $value = array_shift(entity_load('user', array($item[$key])));
            break;
          case 'tid':
            $value = array_shift(entity_load('taxonomy_term', array($item[$key])));
            break;
          case 'vid':
            $value = array_shift(entity_load('taxonomy_vocabulary', array($item[$key])));
            break;
          case 'value':
            $value = $item['value'];
            break;
        }
      }
      else {
        if ($get_key && isset($item[$get_key])) {
          $value = $item[$get_key];
        }
        elseif (array_key_exists('value', $item)) {
          $value = isset($item['safe_value']) ? $item['safe_value'] : $item['value'];
        }
      }
      $values[$delta] = $value;
    }
  }
  if (is_numeric($get_delta)) {
    return isset($values[$get_delta]) ? $values[$get_delta] : NULL;
  }
  return $values;
}

I then created the code below in my node.tpl page to check how many 'body' fields were in the array.

<?php if (render($content)) :
$contArray = array();
$summary = field_fetch_field_values('node', $node, 'body');
$contArray = $summary;
foreach ($contArray as $value) : ?>
<section class="box page-box"> 
<?php print $value; ?>
</section>
<?php endforeach; else : ?>
<section class="box page-box"> 
<h2>There is currently no content for this page.</h2>
</section>  
<?php endif; ?>
share|improve this answer
add comment

Well, first of all, you can inspect the contents of the $content['body'] array using the php native print_r() function.

If the assumptions you have prove to be correct, there is no reason why foreach would not work. However, you can find out the number of an array's elements using the function count(). In this case, you would use it on $content['body'].

share|improve this answer
    
Many thanks for the reply; when i use count($content['body']); i'm getting a value of 26, when there are only 3 instances of the 'body' field. If I use count($content);, it returns a value of 4. How do I target the 'body' field only? –  ss888 May 17 '12 at 16:38
    
Like I said in the reply: use the print_r() function to see what you are dealing with. It is perfectly ok to use it in the template (while developing of course). –  kekkis May 17 '12 at 20:33
    
many thanks for the pointers. –  ss888 May 18 '12 at 9:35
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.