• Resolved fortiernor

    (@fortiernor)


    Hi,

    I need to set the post_title of new pods according to the value entered in other fields. I’ve used the technique described in:

    https://docs.pods.io/code-snippets/create-post-title-from-fields-in-the-post-using-pods_api_pre_save/

    The title text is generated correctly, however the last step, actually setting the post_title, does not seem to have any effect. The Pod in question does support “Title” (advanced options).

    Code:

    private function add_filters() {
            add_filter('pods_api_pre_save_pod_item_membre', array($this, 'set_member_title'), 10, 2);
        }
        public function set_member_title(
            array $pod_array, 
            bool $is_new_item) : array {
            
            if (!$is_new_item) {
                return $pod_array;
            }
            // get field values: ok
            $nom_perso = $this->get_custom_text_field_value($pod_array, 'nom_perso');
            $prenom_perso = $this->get_custom_text_field_value($pod_array, 'prenom_perso');
            $nom_org = $this->get_custom_text_field_value($pod_array, 'nom_org');
            
            $titre = '';
            if (!empty($nom_org)) {
                $titre = $nom_org;
            } else {
                $titre = $prenom_perso . ' ' . $nom_perso;
            }
            
            // debug: ok
            \aco\logging\log('titre: ' . $titre);
            
            if (!empty($titre)) {
                $pod_array['object_fields']['post_title']['value'] = $titre;
            }
            return $pod_array;
        }
    
Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Contributor dan.stefan

    (@danstefan)

    Hey fortiernor,

    You have to make sure post_title is an active field in the pod_array parameter, otherwise it is ignored. Something like this works for example.

    add_filter( 'pods_api_pre_save_pod_item_member', 'set_member_title', 10, 2 );
    
    function set_member_title( array $pod_array, bool $is_new_item ): array {
    
    	if ( ! $is_new_item ) {
    		return $pod_array;
    	}
    
    	// get field values: ok
    	$name      = $pod_array['fields']['lastname']['value'];
    	$firstname      = $pod_array['fields']['firstname']['value'];
    	$org      = $pod_array['fields']['org']['value'];
    
    	if ( ! empty( $org ) ) {
    		$title = $org;
    	} else {
    		$title = $firstname . ' ' . $name;
    	}
    
    	//make sure that post_title is active
    	if ( ! isset( $pod_array['fields_active'][ 'post_title' ] ) ) {
    		$pod_array['fields_active'][] = 'post_title';
    	}
    
    	if ( ! empty( $title )) {
    		$pod_array['object_fields']['post_title']['value'] = $title;
    	}
    
    	return $pod_array;
    }
    
    Plugin Author Jory Hogeveen

    (@keraweb)

    Closing topic due to no reply, feel free to reopen if you still need help!

    Cheers, Jory

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Problem setting pod title from fields on save’ is closed to new replies.