pods-framework / pods

The Pods Framework is a Content Development Framework for WordPress - It lets you create and extend content types that can be used for any project. Add fields of various types we've built in, or add your own with custom inputs, you have total control.
https://pods.io/
GNU General Public License v2.0
1.06k stars 264 forks source link

pods_field_dfv_data filter isn't being applied to a newly created value (only upon a refresh) #5119

Open EthanPhoenix opened 5 years ago

EthanPhoenix commented 5 years ago

Issue Overview

It seems that the "pods_field_dfv_data" filter (similar to "pods_field_pick_data") only manipulate pre-existing values and not newly created ones.

Expected Behavior

Unsure if there is another filter of "post value" creation but one would expect that any display manipulations happening to pre-existing fields will also happen to newly created ones.

Current Behavior

As it currently stands, the newly created field entry will pick the value of the field that is set in the "Title Field" under the "Advanced Options" tab of that related pod.

Steps to Reproduce (for bugs)

To make it convenient, below is the code as a test plugin. `<?php / Plugin Name: Newly Added Field Display Test Plugin URI: Description: Version: 0.0.0 Author: Ethan Phoenix Author URI: http://www.ethanphoenix.tk /

// don't call the file directly if ( !defined( 'ABSPATH' ) ) exit;

add_action( 'plugins_loaded', 'ethansAddon_extend_safe_activate'); function ethansAddon_extend_safe_activate() { if ( defined( 'PODS_VERSION' ) ) {

// MANIPULATING FIELD DISPLAY 
    add_filter('pods_field_dfv_data', 'combobox_display_data', 1, 6);
    function combobox_display_data($data, $args, $attributes){
        switch ($data['fieldConfig']['name']) {
            case 'country':
                foreach ($data['fieldItemData'] as $i=>$val) {
                    $dataid = $data['fieldItemData'][$i]['id'];
                    $countryName = pods_field_display('country', $dataid, 'name', true);
                    $data['fieldItemData'][$i]['name'] = "{$countryName} This text is being added because of a filter but only to an already existing values.";
                }
            break;
        };
        return $data;
    }
}

}`

below image shows a selected value which already existed in the table and thus the text "This text is being added because of a filter but only to an already existing values." is being added to the value.

imag01

this image shows a newly added value "Canada" but as you can see it didn't pass the aforementioned filter and thus only shows the "default" value (a set in the "Title Field" in the related pod's setup screen):

imag02

and here you can see that it does apply but AFTER the entry has been added and we refreshed the page:

imag03

Possible Solution

Either add another "post add" filter or inject the filter again after adding a new value.

WordPress Environment

``` Debug Information WordPress Version: 4.9.8 PHP Version: 7.0.31 MySQL Version: 5.7.23 Server Software: Apache Your User Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:52.0) Gecko/20100101 Firefox/52.0 Session Save Path: C:/Bitnami/WORDPR~1.8-0/php/tmp Session Save Path Exists: Yes Session Save Path Writeable: Yes Session Max Lifetime: 1440 Opcode Cache: Apc: No Memcached: No OPcache: Yes Redis: No Object Cache: APC: No APCu: No Memcache: No Memcached: No Redis: No WPDB Prefix: wp_ WP Multisite Mode: No WP Memory Limit: 40M Pods Network-Wide Activated: No Pods Install Location: C:\Bitnami\wordpress-4.9.8-0\apps\wordpress\htdocs\wp-content\plugins\pods/ Pods Tableless Mode Activated: No Pods Light Mode Activated: No Currently Active Theme: Twenty Seventeen Currently Active Plugins: Newly Added Field Display Test: 0.0.0 Pods - Custom Content Types and Fields: 2.7.9 ```

Pods Package Export (helpful!)

{"meta":{"version":"2.7.9","build":1534559383},"pods":{"5":{"id":5,"name":"country","label":"Countries","description":"","type":"pod","storage":"table","object":"","alias":"","fields":{"name":{"id":6,"name":"name","label":"Name","description":"","help":"","class":"","type":"text","weight":0,"pick_object":"","pick_val":"","sister_id":"","required":"1","unique":"0","text_repeatable":"0","text_allowed_html_tags":"strong em a ul ol li b i","text_max_length":"255","text_placeholder":""},"flag":{"id":7,"name":"flag","label":"Flag","description":"","help":"","class":"","type":"file","weight":1,"pick_object":"custom-simple","pick_val":"","sister_id":"","required":"0","unique":"0","file_format_type":"single","file_uploader":"plupload","file_attachment_tab":"upload","file_edit_title":"1","file_show_edit_link":"1","file_linked":"1","file_limit":"0","file_restrict_filesize":"10MB","file_type":"images","file_field_template":"rows","file_add_button":"Add File","file_modal_title":"Attach a file","file_modal_add_button":"Add File","file_wp_gallery_output":"0","file_wp_gallery_link":"post","file_wp_gallery_columns":"1","file_wp_gallery_random_sort":"0","file_wp_gallery_size":"thumbnail","pick_post_status":["publish"],"admin_only":"0","restrict_role":"0","restrict_capability":"0","hidden":"0","read_only":"0","roles_allowed":["administrator"],"file_allowed_extensions":""}},"show_in_menu":"1","label_singular":"Country","pod_index":"name","hierarchical":"0","ui_style":"post_type","ui_actions_enabled":["add","edit","duplicate","delete"],"ui_reorder_field":"menu_order","ui_fields_manage":{"0":"name","2":"modified"}},"8":{"id":8,"name":"event","label":"Events","description":"","type":"pod","storage":"table","object":"","alias":"","fields":{"name":{"id":9,"name":"name","label":"Name","description":"","help":"","class":"","type":"text","weight":0,"pick_object":"","pick_val":"","sister_id":"","required":"1","unique":"0","text_repeatable":"0","text_allowed_html_tags":"strong em a ul ol li b i","text_max_length":"255","text_placeholder":""},"country":{"id":10,"name":"country","label":"Country","description":"","help":"","class":"","type":"pick","weight":1,"pick_object":"pod","pick_val":"country","sister_id":"","required":"1","unique":"0","pick_format_type":"multi","pick_format_single":"list","pick_format_multi":"list","pick_allow_add_new":"1","pick_taggable":"0","pick_show_icon":"0","pick_show_edit_link":"1","pick_show_view_link":"0","pick_limit":"0","pick_user_role":[],"pick_post_status":["publish"],"admin_only":"0","restrict_role":"0","restrict_capability":"0","hidden":"0","read_only":"0","roles_allowed":["administrator"],"pick_select_text":"","pick_table_id":"","pick_table_index":"","pick_display":"","pick_where":"","pick_orderby":"","pick_groupby":""}},"show_in_menu":"1","label_singular":"Event","pod_index":"name","hierarchical":"0","ui_style":"post_type","ui_actions_enabled":["add","edit","duplicate","delete"],"ui_reorder_field":"menu_order","ui_fields_manage":["name"]}}}

Workaround or Alternate Solution Until Bug is Addressed

Related Issues and/or PRs

pglewis commented 5 years ago

The pods_field_dfv_data filter is only called on page load in order to initialize the starting values for the field, so it was not designed for this.

When you add a record using the "Add New" button, new values are created on the select box dynamically in the browser. I think we can come up with a clean way to do what you're trying to do but it will likely be a job for Javascript and not PHP.

I'm going to close this one in favor of your other ticket, #5091. A proper solution should handle both cases.

pglewis commented 5 years ago

On second thought: re-opening this because there still needs to be a way to hit a filter on add new on the server side, in order to look up custom data. A new filter is called for.