Установите отключенный параметр при предварительном заполнении расширенных настраиваемых полей. - PullRequest
0 голосов
/ 16 апреля 2020

Я предварительно заполняю поле acf select некоторыми вариантами из содержимого моего поста json, используя их документацию.

https://www.advancedcustomfields.com/resources/dynamically-populate-a-select-fields-choices/

Это мой код ниже, который устанавливает первый параметр как null ...

// disabled choice
$field['choices']['null'] = 'Select quote item...';

Но даже если в настройках моего поля выбора allow null имеет значение false Вот как выглядит мое поле в сообщении администратора ...

enter image description here

Первый вариант не отключен. Можно ли действительно установить первый параметр как disabled так или иначе, чтобы он выглядел следующим образом ...

enter image description here

Вот моя полная функция и фильтр код ниже.

/**
 * order constructor method.
 */
public function __construct ()
{

    // order installation price quote sub field item
    add_filter('acf/load_field/key=field_5e97242aa0e56',[$this,'acf_load_installation_items'],20,2);

}

/**
 * pre populate select field with order contents
 * @param array $field
 * @return array $field
 */
public function acf_load_installation_items( $field ) {

    // get the post
    global $post;

    // reset choices
    $field['choices'] = [];

    // disabled choice
    $field['choices']['null'] = 'Select quote item...';

    // decode the order json
    $order = json_decode($post->post_content, true);

    // loop through array and add to field 'choices'
    if(is_array($order)) {

        // loop through order items
        foreach( $order['items'] as $item ) {

            // recreate our value key product id and option id
            $value = $item['id'] . '.' . $item['option_id'];

            // build the label for the choices
            $label = implode(' - ',[
                $item['brand'],
                $item['description'],
                $item['option'],
                'x'.$item['qty']
            ]);

            // build the the custom choice field array
            $field['choices'][ $value ] = $label;

        }

    }

    // return the field
    return $field;

}
...