0

I'm creating a custom file field display formatter module.. https://www.drupal.org/sandbox/vincer/2779871

Right now it's just printing the URI as..

public://my-file.mid

I'm trying to get the default output that I would get by using the File module - a full path as a link, like..

http://my-site/sites/default/files/my-file.mid

The field_formatter_view looks like..

function fieldmidity_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $settings = $display['settings'];
  $element = array();  

  if ($display['type'] == 'midi_formatter') {
    foreach ($items as $delta => $item) {
      $element[$delta] = array('#markup' => $item['uri']);
      // Options for $item; filename, uri, description, filesize, timestamp, fid, uid
    }
  }

  return $element;
}

Based on code I see in the File and Field modules, I'm suspecting that the solution might be more complicated than the approach that I'm attempting.

Any thoughts would be appreciated.

1 Answer 1

1

Use this:

function fieldmidity_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $settings = $display['settings'];
  $element = array();  

  if ($display['type'] == 'midi_formatter') {
    foreach ($items as $delta => $item) {
        if ($item['uri']){
            $element[$delta] = array('#markup' => file_create_url($item['uri']));
        }else{ 
            $element[$delta] = array('#markup' => $item['uri']);
        }
      // Options for $item; filename, uri, description, filesize, timestamp, fid, uid
    }
  }

  return $element;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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