1

By default Drupal 7 output attached files: file name and size of attachment I heed to add an additional column with Download link to every attached file.

In my template.php i have added:

// Override attachments files in case of use table formatter
function bkisk_file_formatter_table($variables) {
$header = array(t('Attachment'), t('Download'), t('Size')); // added Download column
$rows = array();
    foreach ($variables['items'] as $delta => $item) {
        $rows[] = array(
            theme('file_link', array('file' => (object) $item)),           
            theme('file_link', array('file' => (object) $item)), // ???
            format_size($item['filesize']),
        );
    }    
    return empty($rows) ? '' : theme('table', array('header' => $header, 'rows' => $rows));
}

function bkisk_file_link($variables) {
    $file = $variables['file'];
    $icon_directory = $variables['icon_directory'];

    $url = file_create_url($file->uri);
    $icon = theme('file_icon', array('file' => $file, 'icon_directory' => $icon_directory));

    // Set options as per anchor format described at
    $options = array(
        'attributes' => array(
            'type' => $file->filemime . '; length=' . $file->filesize,
        ),
    );

    // Use the description as the link text if available.
    if (empty($file->description)) {
        $link_text = $file->filename;
    }
    else {
        $link_text = $file->description;
        $options['attributes']['title'] = 'откроется в новой вкладке';
        $options['attributes']['target'] = '_blank'; // add _blank param
    }
    return '<span class="file">' . $icon . ' ' . l($link_text, $url, $options) . '</span>';
}

i have http://i.stack.imgur.com/W76kW.png

i need to do like this http://i.stack.imgur.com/cmKMj.png

ill be very appreciated for your help!

  • if form link this way l('Download', $item['uri']), it creates path to file domain.com/public%3A//files/23/ustav_gup_sk_bki_sk_no795_ot_16.06.14.pdf – user34535 Aug 13 '14 at 11:39
2

use File Force Download module to add download functionality to your site. It has field formatter, views integration, good api and etc..

If you want to create a download link programmatically then you can generate download link such way:

l('Linktext', file_force_create_url($image['filepath']), array('query' => array('download' => '1'))).
  • Thank you for your answer. I have installed and activate File Force Download module, then add link formatter programmaticlly to template.php, but it transfer only argument /?download=1 in to address field – user34535 Aug 13 '14 at 11:12
  • i am not agree with you. You're giving wrong path I think – xurshid29 Aug 13 '14 at 11:33
  • try to see what happens when i add this costruction in foreach cycle bkisk.ru/contacts/constituent-documents, it just add additional query to addressbar in browser. i60.tinypic.com/30vdbbm.png – user34535 Aug 13 '14 at 12:17
1

partially solve task by adding to file_link() function construction

function bkisk_file_link($variables) {
    $file = $variables['file'];
    $icon_directory = $variables['icon_directory'];

    $url = file_create_url($file->uri);
    $icon = theme('file_icon', array('file' => $file, 'icon_directory' => $icon_directory));

    // Set options as per anchor format described at
    $options = array(
        'attributes' => array(
            'type' => $file->filemime . '; length=' . $file->filesize,
        ),
    );

    // Use the description as the link text if available.
    if (empty($file->description)) {
        $link_text = $file->filename;
    }
    else {
        $link_text = $file->description;
        $options['attributes']['title'] = 'откроется в новой вкладке';
        $options['attributes']['target'] = '_blank'; // add _blank param

        $options_download['attributes']['title'] = 'Скачать ' . "[" . $link_text . "]";
        $options_download['attributes']['download'] = '';
    }
    return '<span class="file">' . $icon . ' ' . l($link_text, $url, $options) . '</span><br /><br />' .
           '<span class="file-download">' . l('Download', $url, $options_download) . '</span>';
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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