• Resolved RenewMyID

    (@renewmyid)


    Hi,

    I’m using the filter: gfexcel_renderer_filename to change the filename of the Excel file which will be sent as attachment.
    But i’m trying to change the filename depending on a selected value of a dropdown in the form. Is this possible? If so how can i get the value to use in the filter?

    Thanks in advance!

Viewing 11 replies - 1 through 11 (of 11 total)
  • Plugin Author Doeke Norg

    (@doekenorg)

    Hi @renewmyid,

    This is really tricky. You’d have to register a listener for gform_notification (Priority less then 10) and store the desired information for that entry in a global accessible variable (or constant). Then you can read that value within your gfexcel_renderer_filename filter.

    I’d suggest you make a signle class that contains both filters, so you can store the info on a variable on that class. That way you know it is available.

    Do you think you can build this yourself? (I’m not able to give you a working and tested piece of code right now).

    Let me know.

    Plugin Author Doeke Norg

    (@doekenorg)

    Ok so that didn’t work. I’ve been looking into updating the plugin to receive an entry as a param for the filename, but honestly; I don’t see this happening.

    So enter my dirtiest code ever, but I’m darned proud of it ??

    add_filter('gform_notification', function ($notification, $form, $entry) {
        if (isset($notification['attachments']) && count($notification['attachments']) > 0) {
            //probably need to build in some check if this the correct attachment; but asuming you only use this one.
            $old_filename = $notification['attachments'][0]; // this is acutally the full path to the file.
            $new_filename = get_temp_dir() . 'new_filename.xls'; // base this on your $entry value.
    
            // rename the file.
            move_uploaded_file($old_filename, $new_filename);
            // overwrite the file location for the attachment.
            $notification['attachments'][0] = $new_filename;
        }
    
        return $notification;
    }, 20, 3);

    We just rename the file just before we send the file over. This hook HAS the entry data available, so you can use that data anyway you want.

    Hope this helps you out; please let me know.

    Thread Starter RenewMyID

    (@renewmyid)

    Hi Doeke,

    Awesome! Thanks for your amazingly fast support!
    I will try this out today, and I’ll let you know! Thanks!

    Thread Starter RenewMyID

    (@renewmyid)

    Hi Doeke,

    I tried the code, but also with some adjustment. The attachment goes missing from the email ?? it isn’t send with the email.

    But I like the approach, and tried something else. But this also doesn’t seem to work. No attachment is there. Any ideas?

    What i tried but didnt give an attachment in the mail:

    
    add_filter( 'gform_pre_send_email', function ( $email, $message_format, $notification ) {
     
        $old_filename = $email['attachments'][0];
    	$new_filename = get_temp_dir() . 'test_excel.xlsx';
    	
    	move_uploaded_file($old_filename, $new_filename);
    	
    	$email['attachments'][0] = $new_filename;
    	$notification['attachments'][0] = $new_filename;
    	
    	//print_r($email);
    	//print_r($notification);
    	//die();
    	
        return $email;
    }, 10, 3 );
    
    Plugin Author Doeke Norg

    (@doekenorg)

    You need to set the priority to 20.

    So it should end with }, 20, 3);. It has to be triggered later then the plugin. You are triggering it earlier now ??

    Thread Starter RenewMyID

    (@renewmyid)

    I tried it again, but too bad, still no attachment.
    I can’t seem to get it working.

    Any ideas, does it work for you?

    Plugin Author Doeke Norg

    (@doekenorg)

    The code I provided worked. Perhaps you can copy that and then make the adjustments ??.

    Otherwise I’d need access to your site to make it work. But the provided example works 100%.

    Ps..you are also using the wrong hook.

    • This reply was modified 5 years, 6 months ago by Doeke Norg. Reason: Wrong hook
    Thread Starter RenewMyID

    (@renewmyid)

    Hi Doeke,

    I tried your code, exactly like you posted :-). But then I also didn’t get an attachment.
    And then i was so stubborn to try the other hook, but still didn’t work. Parhaps i’m missing something.

    If you want, i could provide you login to the website or FTP access. How can i send you this?

    Either way, thanks! Great and fast support so far!

    Plugin Author Doeke Norg

    (@doekenorg)

    Hi @renewmyid,

    Hmm. that is super weird.

    You can send me the info to [email protected].

    Please also provide the id of the form you are working with, and a login for wordpress where I can manage this form, to make sure the settings are correct.

    Let’s fix this! (later tonight; working now :-))

    Thread Starter RenewMyID

    (@renewmyid)

    Thanks, I just send you an email. No rush needed ??

    Thread Starter RenewMyID

    (@renewmyid)

    Thanks for your help and amazing service! It works ??

    Final solution:

    
    add_filter('gform_notification', function ($notification, $form, $entry) {
        if (isset($notification['attachments']) && count($notification['attachments']) > 0) {
            //probably need to build in some check if this the correct attachment; but asuming you only use this one.
            $old_filename = $notification['attachments'][0]; // this is acutally the full path to the file.
            $new_filename = get_temp_dir() . 'new_filename.xlsx'; // base this on your $entry value.
    
            // rename the file.
            if (rename($old_filename, $new_filename)) {
        	    // overwrite the file location for the attachment, if the move was successfull.
    	        $notification['attachments'][0] = $new_filename;
            }
        }
          
        return $notification;
    }, 20, 3);
    
Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Filename using value of entry’ is closed to new replies.