name = 'swatch'; $this->label = __('Color Swatch', 'acf'); $this->category = 'choice'; $this->defaults = array( 'layout' => 'vertical', 'choices' => array(), 'default_value' => '', 'other_choice' => 0, 'save_other_choice' => 0, 'return_format' => 'value' ); $this->settings = array( 'basename' => plugin_basename(__FILE__), 'path' => apply_filters('acf/swatch_settings/path', plugin_dir_path(__FILE__)), 'url' => apply_filters('acf/swatch_settings/url', plugin_dir_url(__FILE__)), 'version' => '1.0.0' ); // do not delete! parent::__construct(); } /* * render_field() * * Create the HTML interface for your field * * @param $field (array) the $field being rendered * * @type action * @since 3.6 * @date 23/01/13 * * @param $field (array) the $field being edited * @return n/a */ function render_field($field) { // vars $i = 0; $e = ''; $ul = array( 'class' => 'acf-swatch-list', 'data-allow_null' => $field['allow_null'], 'data-other_choice' => $field['other_choice'] ); // append to class $ul['class'] .= ' ' . ($field['layout'] == 'horizontal' ? 'acf-hl' : 'acf-bl'); $ul['class'] .= ' ' . $field['class']; // select value $checked = ''; $value = strval($field['value']); // selected choice if (isset($field['choices'][$value])) { $checked = $value; // custom choice } elseif ($field['other_choice'] && $value !== '') { $checked = 'other'; // allow null } elseif ($field['allow_null']) { // do nothing } else { // select first input by default $checked = key($field['choices']); } // ensure $checked is a string (could be an int) $checked = strval($checked); // other choice if ($field['other_choice']) { // vars $input = array( 'type' => 'text', 'name' => $field['name'], 'value' => '', 'disabled' => 'disabled' ); // select other choice if value is not a valid choice if ($checked === 'other') { unset($input['disabled']); $input['value'] = $field['value']; } // append other choice $field['choices']['other'] = '