Make WordPress Core

Changeset 31006


Ignore:
Timestamp:
12/30/2014 09:30:16 PM (12 years ago)
Author:
boonebgorges
Message:

In wp_dropdown_categories(), allow the term field used to populate option valuesto be specified.

The new 'value_field' parameter makes it possible to set term slugs (or some
other term property) as the 'value' attribute of the option elements generated
by wp_dropdown_categories(). This additional flexibility reduces the effort
required to translate term_id to other term fields when processing form
submissions that include values from taxonomy dropdowns. See #30865 for a
use case.

Props collinsinternet.
Fixes #30306.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/category-template.php

    r31005 r31006  
    294294 *
    295295 * @since 2.1.0
     296 * @since 4.2.0 Introduced the 'value_field' parameter.
    296297 *
    297298 * @param string|array $args {
    … …  
    328329 *                                           Defaults to the value of $name.
    329330 *     @type string       $class             Optional. Value for the 'class' attribute of the select element.
    330  *     @type int          $selected          Optional. ID of the category to be selected.
     331 *     @type int|string   $selected          Optional. Value of the option that should be selected.
     332 *     @type string       $value_field       Optional. Term field that should be used to populate the 'value' attribute
     333 *                                           of the option elements. Accepts any valid term field: 'term_id', 'name',
     334 *                                           'slug', 'term_group', 'term_taxonomy_id', 'taxonomy', 'description',
     335 *                                           'parent', 'count'. Default 'term_id'.
    331336 *     @type string       $taxonomy          Optional. Name of the category to retrieve. Default 'category'.
    332337 *     @type bool         $hide_if_empty     Optional. True to skip generating markup if no categories are found.
    … …  
    347352                'class' => 'postform', 'depth' => 0,
    348353                'tab_index' => 0, 'taxonomy' => 'category',
    349                 'hide_if_empty' => false, 'option_none_value' => -1
     354                'hide_if_empty' => false, 'option_none_value' => -1,
     355                'value_field' => 'term_id',
    350356        );
    351357
    … …  
    11071113         * @param object $category Category data object.
    11081114         * @param int    $depth    Depth of category. Used for padding.
    1109          * @param array  $args     Uses 'selected' and 'show_count' keys, if they exist. @see wp_dropdown_categories()
     1115         * @param array  $args     Uses 'selected', 'show_count', and 'value_field' keys, if they exist.
     1116         *                         See {@see wp_dropdown_categories()}.
    11101117         */
    11111118        public function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 ) {
    … …  
    11151122                $cat_name = apply_filters( 'list_cats', $category->name, $category );
    11161123
    1117                 $output .= "\t<option class=\"level-$depth\" value=\"".$category->term_id."\"";
     1124                if ( ! isset( $args['value_field'] ) || ! isset( $category->{$args['value_field']} ) ) {
     1125                        $args['value_field'] = 'term_id';
     1126                }
     1127
     1128                $output .= "\t<option class=\"level-$depth\" value=\"" . esc_attr( $category->{$args['value_field']} ) . "\"";
     1129
    11181130                if ( $category->term_id == $args['selected'] )
    11191131                        $output .= ' selected="selected"';
  • trunk/tests/phpunit/tests/category.php

    r28706 r31006  
    247247        }
    248248
     249        /**
     250         * @ticket 30306
     251         */
     252        public function test_wp_dropdown_categories_value_field_should_default_to_term_id() {
     253                // Create a test category.
     254                $cat_id = $this->factory->category->create( array(
     255                        'name' => 'Test Category',
     256                        'slug' => 'test_category',
     257                ) );
     258
     259                // Get the default functionality of wp_dropdown_categories().
     260                $dropdown_default = wp_dropdown_categories( array(
     261                        'echo' => 0,
     262                        'hide_empty' => 0,
     263                ) );
     264
     265                // Test to see if it returns the default with the category ID.
     266                $this->assertContains( 'value="' . $cat_id . '"', $dropdown_default );
     267        }
     268
     269        /**
     270         * @ticket 30306
     271         */
     272        public function test_wp_dropdown_categories_value_field_term_id() {
     273                // Create a test category.
     274                $cat_id = $this->factory->category->create( array(
     275                        'name' => 'Test Category',
     276                        'slug' => 'test_category',
     277                ) );
     278
     279                // Get the default functionality of wp_dropdown_categories().
     280                $found = wp_dropdown_categories( array(
     281                        'echo' => 0,
     282                        'hide_empty' => 0,
     283                        'value_field' => 'term_id',
     284                ) );
     285
     286                // Test to see if it returns the default with the category ID.
     287                $this->assertContains( 'value="' . $cat_id . '"', $found );
     288        }
     289
     290        /**
     291         * @ticket 30306
     292         */
     293        public function test_wp_dropdown_categories_value_field_slug() {
     294                // Create a test category.
     295                $cat_id = $this->factory->category->create( array(
     296                        'name' => 'Test Category',
     297                        'slug' => 'test_category',
     298                ) );
     299
     300                // Get the default functionality of wp_dropdown_categories().
     301                $found = wp_dropdown_categories( array(
     302                        'echo' => 0,
     303                        'hide_empty' => 0,
     304                        'value_field' => 'slug',
     305                ) );
     306
     307                // Test to see if it returns the default with the category slug.
     308                $this->assertContains( 'value="test_category"', $found );
     309        }
     310
     311        /**
     312         * @ticket 30306
     313         */
     314        public function test_wp_dropdown_categories_value_field_should_fall_back_on_term_id_when_an_invalid_value_is_provided() {
     315                // Create a test category.
     316                $cat_id = $this->factory->category->create( array(
     317                        'name' => 'Test Category',
     318                        'slug' => 'test_category',
     319                ) );
     320
     321                // Get the default functionality of wp_dropdown_categories().
     322                $found = wp_dropdown_categories( array(
     323                        'echo' => 0,
     324                        'hide_empty' => 0,
     325                        'value_field' => 'foo',
     326                ) );
     327
     328                // Test to see if it returns the default with the category slug.
     329                $this->assertContains( 'value="' . $cat_id . '"', $found );
     330        }
    249331}
Note: See TracChangeset for help on using the changeset viewer.