PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

My custom upload field is not posting correctly

  1. I am creating a field type that uploads a video file to Youtube upon post submission. I plan to use a file upload input type, and upon submission grab the file data (or url/pathname) and upload it to Youtube. This is part of a client project that I have to build in three weeks.

    I've created a custom field by using the following hooks:

    add_action("gform_field_input", array('GFYoutubeUpload', 'add_gf_fieldtype'), 10, 5);
    add_filter("gform_editor_js_set_default_values", array('GFYoutubeUpload', 'add_gf_def
    add_action("gform_editor_js", array('GFYoutubeUpload', 'add_gf_formeditor'));
    add_filter("gform_field_type_title", array('GFYoutubeUpload', 'add_gf_title'));
    add_filter("gform_add_field_buttons", array('GFYoutubeUpload', 'add_gf_button'));

    (I also have a pre_submission handler that I've begun to write.)

    I've created a File input type, and I believe that I have the correct naming convention. I've posted my function below for review.) The input type is displaying correctly on the admin page and the form preview, but when I post the form, my custom field doesn't have a value associated with it. (There's nothing in $_POST corresponding to it, either.)

    Looking at the code below, can anybody tell me where I've gone wrong creating this input? Is there another hook that I don't know about that I need to use to handle the data I've added to the form?

    /* Register the cusitom field type with Gravity Forms. */
        public static function add_gf_fieldtype($input, $field, $value, $lead_id, $form_id) {
            if ($field["type"] == "youtube") {
                $input_name = $form_id .'_'. $field['id'];
                $css = isset($field['cssClass']) ? $field['cssClass'] : '';
    
                return sprintf("<div class='ginput_container'><input name='input_%s' id='input_%s_%s' class='%s' value='' type='file' /></div>", $field["id"], $form_id, $field["id"], $field["type"]. ' ' .esc_attr($css). ' ' .$field['size']);
            }
    
            return $input;
        }

    Thank you.

    Posted 12 years ago on Saturday April 14, 2012 | Permalink
  2. The problem is that file upload inputs dont post their values in the $_POST variable, you need to use the $_FILES variable to access submitted file upload fields

    Posted 12 years ago on Tuesday April 17, 2012 | Permalink
  3. Thank you for your reply.

    You were right - I was able to find the file in the $_FILES superglobal, and I'm now able to upload it to Youtube. I've run into another issue, though - my field entry isn't displaying in the Entries screen, even though I'm explicitly adding the Youtube URL to the $entry object in post submission. See the code below.

    /* Pre-submission hook: grab the Youtube field values and send them
         * to the Youtube class. */
        public static function post_gf_youtubefields($form) {
            $yt_field_array = array();
    
            foreach($form["fields"] as $field) {
                if ($field["type"] == "youtube") {
                    $yt_field_array[] = $field;
                }
            }
    
            foreach($yt_field_array as $field) {
                // Upload the file to Youtube.
                $yt_file =  $_FILES['input_'.$field['id']]['tmp_name'];
                echo $yt_file;
                $yt_video_url = GFYoutubeUpload::upload_to_youtube($yt_file);
    
                // Save the field ID and URL as a key-value pair for later use.
                self::$entry_ids[$field['id']] = $yt_video_url;
    
                if (file_exists($_FILES['input_'.$field['id']]['tmp_name'])) {
                    unlink($yt_file);
                }
            }
        }
    
        /* Post-submission hook: write the Youtube URL to the now-available entry object. */
        public static function write_yt_url($entry, $form) {
            foreach (self::$entry_ids as $key => $value) {
                $entry[$key] = $value;
            }
        }

    I've been looking into functions like gforms_entries_field_value and gform_entry_field_value, but it doesn't look as though I can grab anything. (In fact, I just poked around in the wp_rg_lead databases, and it doesn't look as though any value is grabbed.)

    Because I'm using a file upload field, do I need to do an explicit write to the wp_rg_lead_detail database with my data? Or is the data stored/captured elsewhere?

    Again, I appreciate all of the help.

    Edit: If this would be better as a separate post, let me know.

    Posted 12 years ago on Wednesday April 18, 2012 | Permalink
  4. Yes, the values from custom file upload fields won't automatically be added to the wp_rg_lead_detail table. Inserting it manually in the gform_after_submission hook should do the trick for you. Just remember that the field_number column should match your field ID.

    Posted 12 years ago on Thursday April 19, 2012 | Permalink
  5. I see. Is there a Gravity Forms API that I can use to write to that table, or will I have to use the wpdb class?

    Posted 12 years ago on Thursday April 19, 2012 | Permalink
  6. Sorry for the delay. There aren't any lower level Gravity Forms API methods that you can use to specifically write to that table, so yea, you will need to use the wpdb class

    Posted 11 years ago on Monday April 30, 2012 | Permalink