Search code examples
wordpresspodscms

How to change output of a certain Pods field?


I have two pods: course and teacher.

Each course has a teacher.

I use shortcodes in order to build a form to define new course:

[pods name='course' form='1' fields='name, teacher' ]

When defining a new course, the user can choose the teacher for this course.

By default, the name of the teacher is displayed in a drop down list. I wonder if I can change the output of the teachers in the drop down list.

For example, in addition to the name I want to display a certain field, such as location of the teacher in the drop down list.

Is this possible using the built-in shortcodes of Pods 2?


Update:

Following the instructions of Scott, I solved the problem. I wrote the solution into the comment section but formating was lost. Below, I put the code again:

function pods_teacher_pick_data($data, $name, $value, $options, $pod, $id){
    if ($name == "pods_field_teachers") {
        foreach ($data as $id => &$value) {
            $p = pods('teacher', $id);
            $name = $p->display('name');
            $city = $p->display('profile.city.name');
            $value = $name . ' - ' . $city;
        }
    }
    return $data;
}

add_filter('pods_field_pick_data', 'pods_teacher_pick_data', 1, 6);

Solution

  • Not built in yet, but you can take over the data output using the filter: pods_field_pick_data

    $data = apply_filters( 'pods_field_pick_data', $data, $name, $value, $options, $pod, $id );
    

    Adding a filter to that filter should give you the ability to change what appears in the drop-down, or other relationship input types.

    Edit: I just added a similar filter for filtering the autocomplete data too.

    $pick_data = apply_filters( 'pods_field_pick_data_ajax', array(), $field[ 'name' ], null, $field, $pod, 0, $data );
    

    $data in this array is actually the fully setup PodsData object

    EDIT (02/07/2013):

    In Pods 2.3, I've added a quick function that should streamline adding custom relationship objects. This is preferrer over overriding an existing relationship or using the custom simple definition dynamically. Pretty easy to use, check it out at https://github.com/pods-framework/pods/issues/1033

    $options = array(
        'group' => 'Special Relationships', // Whatever you want the selection group to be, defaults to label
        'simple' => false, // Whether this field is related by strings or integer IDs, integer IDs get stored in wp_podsrel, strings are stored in the field itself either serialized (meta-based) or json encoded (table-based)
        'data' => array( // Custom define the items to select from manually
            1 => 'Gravity Form 1',
            2 => 'Gravity Form 2'
        ),
        'data_callback' => 'get_custom_gravity_forms_list', // Provide a callback function to call to define the data (instead of setting 'data' above)
        'value_to_label_callback' => 'get_custom_gravity_forms_list', // Provide a callback function to define the data when called through PodsField_Pick::value_to_label
        'simple_value_callback' => 'get_custom_gravity_forms_list' // Provide a callback function to define the data when called through PodsField_Pick::simple_value
    );
    
    pods_register_related_object( 'gravity-form', 'Gravity Forms', $options );