-
Notifications
You must be signed in to change notification settings - Fork 260
Expand file tree
/
Copy pathSet_Field.php
More file actions
98 lines (82 loc) · 2.07 KB
/
Copy pathSet_Field.php
File metadata and controls
98 lines (82 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php
namespace Carbon_Fields\Field;
use Carbon_Fields\Helper\Helper;
use Carbon_Fields\Value_Set\Value_Set;
/**
* Set field class.
* Allows to create a set of checkboxes where multiple can be selected.
*/
class Set_Field extends Predefined_Options_Field {
/**
* Default field value
*
* @var array
*/
protected $default_value = array();
/**
* The options limit.
*
* @var int
*/
protected $limit_options = 0;
/**
* Allowed HTML attributes for the field.
*
* @var string[]
*/
protected $allowed_attributes = [ 'disabled' ];
/**
* Create a field from a certain type with the specified label.
*
* @param string $type Field type
* @param string $name Field name
* @param string $label Field label
*/
public function __construct( $type, $name, $label ) {
$this->set_value_set( new Value_Set( Value_Set::TYPE_MULTIPLE_VALUES ) );
parent::__construct( $type, $name, $label );
}
/**
* {@inheritDoc}
*/
public function set_value_from_input( $input ) {
if ( ! isset( $input[ $this->get_name() ] ) ) {
return $this->set_value( array() );
}
$options_values = $this->get_options_values();
$value = stripslashes_deep( $input[ $this->get_name() ] );
$value = Helper::get_valid_options( $value, $options_values );
return $this->set_value( $value );
}
/**
* {@inheritDoc}
*/
public function to_json( $load ) {
$field_data = parent::to_json( $load );
$options = $this->parse_options( $this->get_options(), true );
$value = array_map( 'strval', $this->get_formatted_value() );
$field_data = array_merge( $field_data, array(
'options' => $options,
'value' => $value,
'limit_options' => $this->limit_options,
) );
return $field_data;
}
/**
* {@inheritDoc}
*/
public function get_formatted_value() {
$value = $this->get_value();
$value = $this->get_values_from_options( $value );
return $value;
}
/**
* Set the number of the options to be displayed at the initial field display.
*
* @param int $limit
*/
public function limit_options( $limit ) {
$this->limit_options = $limit;
return $this;
}
}