PodsForm::options PHP Method

options() public static method

Setup options for a field and store them for later use
Since: 2.0
public static options ( $type, $options ) : array
$type
$options
return array
    public static function options($type, $options)
    {
        $options = (array) $options;
        if (!is_object($options) && isset($options['options'])) {
            $options_temp = $options['options'];
            unset($options['options']);
            $options = array_merge($options_temp, $options);
            $override = array('class');
            foreach ($override as $check) {
                if (isset($options_temp[$check])) {
                    $options[$check] = $options_temp[$check];
                }
            }
        }
        $defaults = self::options_setup($type, $options);
        $core_defaults = array('id' => 0, 'label' => '', 'description' => '', 'help' => '', 'default' => null, 'attributes' => array(), 'class' => '', 'grouped' => 0);
        $defaults = array_merge($core_defaults, $defaults);
        foreach ($defaults as $option => $settings) {
            $default = $settings;
            if (is_array($settings) && isset($settings['default'])) {
                $default = $settings['default'];
            }
            if (!isset($options[$option])) {
                $options[$option] = $default;
            }
        }
        return $options;
    }

Usage Example

/**
 * Pre-populate options for bound fields
 *
 * @since 1.0.0
 *
 * @param array $field Field config
 *
 * @return array Field config
 */
function pods_cf_populate_options($field)
{
    global $form;
    $processors = Caldera_Forms::get_processor_by_type('pods', $form);
    if (empty($processors)) {
        return $field;
    }
    foreach ($processors as $processor) {
        // is configured
        $fields = array();
        if (!empty($processor['config']['fields'])) {
            $fields = array_merge($fields, $processor['config']['fields']);
        }
        if (!empty($processor['config']['object_fields'])) {
            $fields = array_merge($fields, $processor['config']['object_fields']);
        }
        if ($bound_field = array_search($field['ID'], $fields)) {
            // now lets see if this is a pick field
            $pod = pods($processor['config']['pod'], null, false);
            $pod_field = $pod->fields($bound_field);
            if (!empty($pod_field['options']['required'])) {
                $field['required'] = 1;
            }
            if ($pod_field['type'] === 'pick') {
                $options = PodsForm::options($pod_field['type'], $pod_field);
                include_once PODS_DIR . 'classes/fields/pick.php';
                $fieldtype = new PodsField_Pick();
                $choices = $fieldtype->data($bound_field, null, $options, $pod);
                $field['config']['option'] = array();
                foreach ($choices as $choice_value => $choice_label) {
                    $field['config']['option'][] = array('value' => $choice_value, 'label' => $choice_label);
                }
            }
        }
    }
    return $field;
}