How to add fields wrapper in field preprocess My code:

function MYTHEME_preprocess_field(&amp;$vars, $hook) {
  $element = $vars['element'];
  if($vars['element']['#field_name'] == 'field_example') {
    foreach($vars['items'] as $key =&gt; $item) {
      $vars['items'][$key]['#prefix'] = '&lt;figure class="col-xs-4 col-sm-3 col-md-2"&gt;';
      $vars['items'][$key]['#suffix'] = '&lt;figcaption&gt;' . $item['#item']['alt'] . '&lt;/figcaption&gt;';
      $vars['items'][$key]['#suffix'] .= '&lt;/figure&gt;';
    }
  }
}


I want to wrap all items in a div. I should use some code outside the foreach, but I hadn't found any solution. Can you help me?
Based on the question I'm assuming you are asking for D7 ...

You should use a "container" for this. Something like this:

$element = $vars['element'];
if($vars['element']['#field_name'] == 'field_example') {
  foreach($vars['items'] as $key =&gt; $item) {
    // Do whatever you want...
  }
  $vars['items']['#type'] = 'container';
  $vars['items']['#attributes'] = array(
    'id' =&gt; "some-id",
    'class' =&gt; array(
      'some-class',
    ),
  );
}


See this other question for more details. Also, if you want to get a little more fancy you can look at using theme wrappers.