Skip to content

Instantly share code, notes, and snippets.

@EtienneRd
Created November 23, 2012 10:28
Show Gist options
  • Save EtienneRd/4134954 to your computer and use it in GitHub Desktop.
Save EtienneRd/4134954 to your computer and use it in GitHub Desktop.
Drupal:Format download file link
/**
* Implements hook_field_formatter_info()
*/
function MYMODULE_field_formatter_info()
{
return array(
// Format de téléchargement de fichier
'MYMODULE_file_link' => array(
'label' => t('Download link'),
'field types' => array('file')
),
);
}
/**
* Implements hook_field_formatter_view()
*/
function MYMODYLE_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display)
{
$element = array();
$settings = $display['settings'];
switch ($display['type']) {
case 'visio_custom_file_link':
$list = array();
// Manage several files
foreach ($items as $file) {
// Get file extension
$ext = end(explode('.', $file['filename']));
// Generate link
$list[] = array(
'data' => l(t('Download'), file_create_url($file['uri']), array(
'attributes' => array(
'class' => $ext . '-download-link',
'title' => $file['filename'],
'alt' => $file['filename'],
))),
);
}
$element[0] = array(
'#theme' => 'item_list',
'#items' => $list,
'#attributes' => array('class' => 'download-links-list')
);
break;
}
return $element;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment