Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
molsov opened this issue May 6, 2021 · 5 comments

Comments

@molsov
Copy link

molsov commented May 6, 2021

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

@dlh01
Copy link
Member

dlh01 commented May 7, 2021

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?

@molsov
Copy link
Author

molsov commented May 7, 2021

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
Copy link
Author

molsov commented May 7, 2021

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

@dlh01
Copy link
Member

dlh01 commented May 8, 2021

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.

@molsov
Copy link
Author

molsov commented May 9, 2021

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants