Hi all,

right now I have this function in my module to format a special kind of file type:

function jmol_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();
  $settings = $display['settings'];
  if ($display['type'] == 'jmol') {
    // Get the Jmol javascript library path.
    $jmoldir = libraries_get_path('jmol');
    $library = libraries_load('jmol');
    drupal_add_js($jmoldir . '/Jmol.js');
    $css = '';
    // Loop over multiple file attachments.
    foreach ($items as $delta => $item) {
      // We need to grab the full file url for our applet.
      $url = file_create_url($item['uri']);
      // Build the css to set the applet alignment.
      $css .= jmol_build_css($settings);
      // Let a custom function build the html for this field.
      $html = jmol_build_html($settings, $url, $jmoldir, $delta);
      $element[$delta] = array('#markup' => $html);
    }
    // Add css.
    drupal_add_css($data = $css, $type = 'inline');
  }
  return $element;
}

The case of a simple file field, without field collection:
the $delta variable is important to me since it indicates how many files a user has uploaded to one node. Then I pass this $delta to jmol_build_html() and I do something with that $delta value for each file.
This works fine as the hook_field_formatter_view() is called only once and I loop over multiple file attachments.

The case of a field collection:
I made a field collection including a file field and allow multiple values for the entire field collection. To be precise, the collection is a textfield and a file field, this allows users to create text around the formatted file(s).
What happens now is that hook_field_formatter_view() is called upon each field collection value.
This implies that when I add e.g. 3 values (so each with a textfield and a file, 3 textfields and 3 files in total), the $delta value remains 0, since the hook is called 3 times. Hence, I cannot pinpoint how many files I have in the collection.

Is this clear enough for what I want to accomplish? :)

Joost

Comments

jvdurme’s picture

Bump. No one can help me here? Formatters are pretty useful.