Code Duplication    Length = 37-38 lines in 2 locations

core/Field/Multiselect_Field.php 1 location

@@ 8-44 (lines=37) @@
5
/**
6
 * Select dropdown field class wtih multiple selection enabled.
7
 */
8
class Multiselect_Field extends Predefined_Options_Field {
9
	/**
10
	 * Returns an array that holds the field data, suitable for JSON representation.
11
	 * This data will be available in the Underscore template and the Backbone Model.
12
	 *
13
	 * @param bool $load  Should the value be loaded from the database or use the value from the current instance.
14
	 * @return array
15
	 */
16
	public function to_json( $load ) {
17
		$field_data = parent::to_json( $load );
18
		$this->load_options();
19
20
		$field_data = array_merge( $field_data, array(
21
			'options' => $this->parse_options( $this->options ),
22
		) );
23
		return $field_data;
24
	}
25
26
	/**
27
	 * The main Underscore template of this field.
28
	 */
29
	public function template() {
30
		?>
31
		<# if (_.isEmpty(options)) { #>
32
			<em><?php _e( 'no options', 'carbon_fields' ); ?></em>
33
		<# } else { #>
34
			<select id="{{{ id }}}" name="{{{ name }}}[]" multiple {{{ special_attrs }}}>
35
				<# _.each(options, function(option) { #>
36
                    <option value="{{ option.value }}" {{{ value && ( value.indexOf(option.value) != -1 ) ? 'selected="selected"' : '' }}}>
37
						{{{ option.name }}}
38
					</option>
39
				<# }) #>
40
			</select>
41
		<# } #>
42
		<?php
43
	}
44
}
45

core/Field/Select_Field.php 1 location

@@ 8-45 (lines=38) @@
5
/**
6
 * Select dropdown field class.
7
 */
8
class Select_Field extends Predefined_Options_Field {
9
	/**
10
	 * Returns an array that holds the field data, suitable for JSON representation.
11
	 * This data will be available in the Underscore template and the Backbone Model.
12
	 *
13
	 * @param bool $load  Should the value be loaded from the database or use the value from the current instance.
14
	 * @return array
15
	 */
16
	public function to_json( $load ) {
17
		$field_data = parent::to_json( $load );
18
		$this->load_options();
19
20
		$field_data = array_merge( $field_data, array(
21
			'options' => $this->parse_options( $this->options ),
22
		) );
23
24
		return $field_data;
25
	}
26
27
	/**
28
	 * The main Underscore template of this field.
29
	 */
30
	public function template() {
31
		?>
32
		<# if (_.isEmpty(options)) { #>
33
			<em><?php _e( 'no options', 'carbon_fields' ); ?></em>
34
		<# } else { #>
35
			<select id="{{{ id }}}" name="{{{ name }}}" {{{ special_attrs }}} {{{ special_attrs }}} >
36
				<# _.each(options, function(option) { #>
37
					<option value="{{ option.value }}" {{{ option.value == value ? 'selected="selected"' : '' }}}>
38
						{{{ option.name }}}
39
					</option>
40
				<# }) #>
41
			</select>
42
		<# } #>
43
		<?php
44
	}
45
}
46