checkbox.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. /**
  3. * Elgg checkbox input
  4. * Displays a checkbox input tag
  5. *
  6. * @package Elgg
  7. * @subpackage Core
  8. *
  9. *
  10. * Pass input tag attributes as key value pairs. For a list of allowable
  11. * attributes, see http://www.w3schools.com/tags/tag_input.asp
  12. *
  13. * @uses $vars['name'] Name of the checkbox
  14. * @uses $vars['value'] Value of the checkbox
  15. * @uses $vars['default'] The default value to submit if not checked.
  16. * Optional, defaults to 0. Set to false for no default.
  17. * @uses $vars['checked'] Whether this checkbox is checked
  18. * @uses $vars['label'] Optional label string
  19. * @uses $vars['class'] Additional CSS class
  20. * @uses $vars['label_class'] Optional class for the label
  21. */
  22. $vars['class'] = (array) elgg_extract('class', $vars, []);
  23. $vars['class'][] = 'elgg-input-checkbox';
  24. $defaults = array(
  25. 'default' => 0,
  26. 'disabled' => false,
  27. 'type' => 'checkbox'
  28. );
  29. $vars = array_merge($defaults, $vars);
  30. $default = $vars['default'];
  31. unset($vars['default']);
  32. if (isset($vars['name']) && $default !== false) {
  33. echo elgg_view('input/hidden', ['name' => $vars['name'], 'value' => $default]);
  34. }
  35. $label = elgg_extract('label', $vars, false);
  36. $label_class = elgg_extract('label_class', $vars);
  37. unset($vars['label']);
  38. unset($vars['label_class']);
  39. $input = elgg_format_element('input', $vars);
  40. if (!empty($label)) {
  41. echo elgg_format_element('label', ['class' => $label_class], "$input $label");
  42. } else {
  43. echo $input;
  44. }