wordpress-fieldmanager icon indicating copy to clipboard operation
wordpress-fieldmanager copied to clipboard

Fieldmanager_Autocomplete showing cateegory name + slug while adding, disappreas after saving.

Open molsov opened this issue 3 years ago • 5 comments

This is the code

'category' => new Fieldmanager_Autocomplete( array(
								'label'          => 'Category',
								'name'           => 'category',
								'limit'          => 0,
								'extra_elements' => 0,
								'add_more_label' => 'Add Category',
								'display_if'     => [ 'src' => 'filter_type', 'value' => 'category' ],
								'datasource'     => new Fieldmanager_Datasource_Term( array(
									'taxonomy'               => array( 'category' ),
									'taxonomy_save_to_terms' => false
								) )
							) ),

while adding a category, it is showing the slug alongside, but after saving, the slug disappears.

While adding image

after saving image

molsov avatar May 06 '21 22:05 molsov

Hi @molsov, and thanks for the report.

I'm unable to replicate this behavior on the current Fieldmanager master branch. Is there any custom code running that might be adding a filter that includes the slug, such as with the fm_datasource_term_get_items or get_terms filters?

dlh01 avatar May 07 '21 15:05 dlh01

Hi @dlh01, thanks for the reply,

You are right, we are using fm_datasource_term_get_items to edit the label, but after selecting and saving, only the category name stays instead of category name + slug in the Fieldmanager_Autocomplete, But working fine with Fieldmanager_Select

molsov avatar May 07 '21 22:05 molsov

Yeah, I found a fix for this,

using fm_datasource_term_get_value

public static function filter_fm_datasource_term_get_value( $value, $terms, $fm ) {

		if ( is_array( $fm->taxonomy ) && count( $fm->taxonomy ) === 1 ) {
			if ( 'category' === $fm->taxonomy[0] ) {
				return $terms->name . ' (' . $terms->slug . ')' ;
			}
		}

		return $value;
	}

Thanks for the help @dlh01

molsov avatar May 07 '21 23:05 molsov

Before you go, @molsov, could you share any of the code that's filtering fm_datasource_term_get_items? I'd like to see more about why it behaves differently for autocompletes and selects.

dlh01 avatar May 08 '21 00:05 dlh01

@dlh01 yeah sure,


public static function filter_fm_datasource_term_get_items( $stack, $terms, $fm ) {

		if ( is_array( $fm->taxonomy ) && count( $fm->taxonomy ) !== 1 && 'category' !== $fm->taxonomy[0] ) {
			return $stack;
		}
		foreach ( $stack as $term_id => $term_name ) {
			$matching_terms = wp_filter_object_list( $terms, [ 'term_id' => $term_id ] );
			if ( ! empty( $matching_terms ) ) {
				$matching_term = array_shift( $matching_terms );
				$stack[ $term_id ] .= ' (' . $matching_term->slug . ')';
			}
		}
		return $stack;
	}

This is the code that's filtering fm_datasource_term_get_items

Why it behaves differently for autocompletes and selects. That's maybe because select uses the get_items values itself(which are already edited) and autocompletes get the value from id(which the one without category slug). But after introducing the filter_fm_datasource_term_get_value, it is working fine.

molsov avatar May 09 '21 17:05 molsov