Skip to content

Commit

Permalink
Merge pull request #5020 from pods-framework/feature/5014-add-new-rec…
Browse files Browse the repository at this point in the history
…ords-not-selected-docs

Update inline docs for pick field selected logic
  • Loading branch information
sc0ttkclark authored Mar 18, 2020
2 parents ae48f1e + 15e518b commit 6560ff4
Showing 1 changed file with 29 additions and 16 deletions.
45 changes: 29 additions & 16 deletions classes/fields/pick.php
Original file line number Diff line number Diff line change
Expand Up @@ -1304,28 +1304,41 @@ public function build_dfv_field_item_data_recurse_item( $item_id, $item_title, $
$edit_link = add_query_arg( array( 'pods_modal' => '1' ), $edit_link );
}

// Determine if this is a selected item
// Determine if this is a selected item.
// Issue history for setting selected: #4753, #4892, #5014.
$selected = false;

if ( is_array( $args->value ) ) {
if ( ! isset( $args->value[0] ) ) {
$keys = array_map( 'strval', array_keys( $args->value ) );
$values = array();

if ( in_array( (string) $item_id, $keys, true ) ) {
$selected = true;
}
}
// If we have values, let's cast them.
if ( ! empty( $args->value ) ) {
// The value may be a single non-array value.
$values = (array) $args->value;
}

if ( ! $selected ) {
// Cast values in array as string.
$args->value = array_map( 'strval', $args->value );
// Cast values in array as string.
$values = array_map( 'strval', $values );

if ( in_array( (string) $item_id, $args->value, true ) ) {
$selected = true;
}
// If the value array has keys as IDs, let's check for matches from the keys first.
if ( ! isset( $values[0] ) ) {
// Get values from keys.
$key_values = array_keys( $values );

// Cast key values in array as string.
$key_values = array_map( 'strval', $key_values );

// Let's check to see if the current $item_id matches any key values.
if ( in_array( (string) $item_id, $key_values, true ) ) {
$selected = true;
}
}

// If we do not have a key match, the normal values may still match.
if ( ! $selected ) {
// Let's check to see if the current $item_id matches any values.
if ( in_array( (string) $item_id, $values, true ) ) {
$selected = true;
}
} elseif ( (string) $item_id === (string) $args->value ) {
$selected = true;
}

$item = array(
Expand Down

0 comments on commit 6560ff4

Please sign in to comment.