0

Like the title says I want to make managed_file value to be a link on form submissions in the email.

Webform has this option. When a user uploads file, on the email form submission there's a link with the path of the uploaded file. I want to achieve this with custom module. This is my code:

<?php

function mymodule_page($form, &$form_state) {
    $form['description'] = array(
        '#type' => 'markup',
        '#markup' => t('Add description'),
    );
    $form['file'] = array(
        '#type' => 'managed_file', 
        '#title' => t('Upload image'),
        '#upload_location' => 'public://files',

    );
    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Submit'),
        '#submit' => array('mymodule_page_submit'),
    );

    return $form;
}

function mymodule_page_submit($form, &$form_state) {
    $file = file_load($form_state['values']['file']);
    $file->status = FILE_STATUS_PERMANENT;
    file_save($file);

    //dpm($file);

    $description = $form_state['values']['description'];

    $params = array('file' => $file->uri, 'description' => $description ); 
    drupal_mail('mymodule', 'mymodule_news_mail', 'myemail', language_default(), $params);
    drupal_set_message(t('Message sent!')); 
}

/*
** Implements hook_mail().
*/
function mymodule_mail($key, &$message, $params) {
    switch($key) {    
        case 'mymodule_news_mail':
            $message['subject'] = t('Hello');
            $message['body'][] = t('This is a test');
            $message['body'][] = t('Image upload: @file', array('@file' => $params['file']));
            $message['body'][] = t('Description: @description', array('@description' => $params['description']));   
        break;    
    }
}

As you can see it from my code I'm saving the file properly into the server. After that with dpm($file->uri) I can see the string public://files/file_name.

How can I display the link from the path of the uploaded file on the email, so will be able to click on the absolute path?

0

Thanks to Jaypan from drupal.org, I've got the answer:

<?php
file_create_url($file->uri);
?>

Simply with adding this code and make another variable that will use the code, I've got the right result.

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.