LLMS_Form_Field::prepare_options( array $raw )

Prepare the fields options.


Description Description

Allows options to be setup as an associative array of key/value pairs or an array of associative arrays each with a "label" and "key" property. The "key" property may be omitted, in which case the "label" will be duplicated as the option’s "value".


Top ↑

Parameters Parameters

$raw

(array) (Required) Raw field data.


Top ↑

Return Return

(array)


Top ↑

Source Source

File: includes/forms/class-llms-form-field.php

	protected function prepare_options( $raw ) {

		$prepared = array();

		foreach ( $raw as $key => $val ) {

			if ( is_array( $val ) ) {

				// Option group.
				if ( isset( $val['options'] ) ) {

					$prepared[ $key ] = array(
						'label'   => isset( $val['label'] ) ? $val['label'] : $key,
						'options' => $this->prepare_options( $val['options'] ),
					);

					// From block editor options array.
				} elseif ( isset( $val['text'] ) ) {

					$item_key              = isset( $val['key'] ) ? $val['key'] : $val['text'];
					$prepared[ $item_key ] = $val['text'];

					if ( isset( $val['default'] ) && llms_parse_bool( $val['default'] ) ) {
						if ( 'checkbox' === $this->settings['type'] ) { // Account for multiple defaults.
							$this->settings['default']   = is_array( $this->settings['default'] ) ? $this->settings['default'] : array();
							$this->settings['default'][] = $item_key;
						} else {
							$this->settings['default'] = $item_key;
						}
					}
				}

				// Flat array of $key=>$val.
			} else {

				$prepared[ $key ] = $val;

			}
		}

		// Add a placeholder.
		if ( $this->settings['placeholder'] ) {
			$this->settings['default'] = '';
			$prepared                  = array_merge( array( '' => $this->settings['placeholder'] ), $prepared );
		}

		return $prepared;

	}


Top ↑

Changelog Changelog

Changelog
Version Description
5.0.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.