Completed
Pull Request — master (#34)
by
unknown
03:04
created

Multiselect_Field::template()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 5

Duplication

Lines 15
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 5
c 1
b 0
f 1
nc 1
nop 0
dl 15
loc 15
rs 9.4285
1
<?php
2
3
namespace Carbon_Fields\Field;
4
5
/**
6
 * Select dropdown field class wtih multiple selection enabled.
7
 */
8 View Code Duplication
class Multiselect_Field extends Predefined_Options_Field {
0 ignored issues
show
Duplication introduced by
This class 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...
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"' : '' }}}>
0 ignored issues
show
Coding Style introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
37
						{{{ option.name }}}
38
					</option>
39
				<# }) #>
40
			</select>
41
		<# } #>
42
		<?php
43
	}
44
}
45