Completed
Pull Request — master (#1)
by
unknown
10:02
created

Select_Field::add_options()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 4
rs 10
cc 1
eloc 3
nc 1
nop 1
1
<?php 
2
3
namespace Carbon_Fields\Field;
4
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 View Code Duplication
	public function to_json( $load ) {
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 }}}">
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