NF_Fields_ListCheckbox

Class NF_Fields_CheckboxList.

Defined (1)

The class is defined in the following location(s).

/includes/Fields/ListCheckbox.php  
  1. class NF_Fields_ListCheckbox extends NF_Abstracts_List 
  2. protected $_name = 'listcheckbox'; 
  3.  
  4. protected $_type = 'listcheckbox'; 
  5.  
  6. protected $_nicename = 'Checkbox List'; 
  7.  
  8. protected $_section = 'common'; 
  9.  
  10. protected $_icon = 'list'; 
  11.  
  12. protected $_templates = 'listcheckbox'; 
  13.  
  14. protected $_old_classname = 'list-checkbox'; 
  15.  
  16. public function __construct() 
  17. parent::__construct(); 
  18.  
  19. $this->_nicename = __( 'Checkbox List', 'ninja-forms' ); 
  20.  
  21. add_filter( 'ninja_forms_merge_tag_calc_value_' . $this->_type, array( $this, 'get_calc_value' ), 10, 2 ); 
  22.  
  23. public function admin_form_element( $id, $value ) 
  24. $form_id = get_post_meta( absint( $_GET[ 'post' ] ), '_form_id', true ); 
  25.  
  26. $field = Ninja_Forms()->form( $form_id )->get_field( $id ); 
  27.  
  28. $list = ''; 
  29. foreach( $field->get_setting( 'options' ) as $option ) { 
  30. $checked = ( in_array( $option[ 'value' ], $value ) ) ? "checked" : ''; 
  31. $list .= "<li><label><input type='checkbox' value='{$option[ 'value' ]}' name='fields[$id][]' $checked> {$option[ 'label' ]}</label></li>"; 
  32.  
  33. return "<ul>$list</ul>"; 
  34.  
  35. public function get_calc_value( $value, $field ) 
  36. $selected = explode( ', ', $value ); 
  37. $value = 0; 
  38. if( isset( $field[ 'options' ] ) ) { 
  39. foreach ($field['options'] as $option ) { 
  40. if( ! isset( $option[ 'value' ] ) || ! in_array( $option[ 'value' ], $selected ) || ! isset( $option[ 'calc' ] ) ) continue; 
  41. $value += $option[ 'calc' ]; 
  42. return $value;