/layouts/joomla/form/field/radio/buttons.php

https://github.com/joomla/joomla-cms · PHP · 123 lines · 68 code · 14 blank · 41 comment · 9 complexity · f2e4b6ad3850f5febe8c364aa1b52794 MD5 · raw file

  1. <?php
  2. /**
  3. * @package Joomla.Site
  4. * @subpackage Layout
  5. *
  6. * @copyright (C) 2018 Open Source Matters, Inc. <https://www.joomla.org>
  7. * @license GNU General Public License version 2 or later; see LICENSE.txt
  8. */
  9. defined('_JEXEC') or die;
  10. extract($displayData);
  11. /**
  12. * Layout variables
  13. * -----------------
  14. * @var string $autocomplete Autocomplete attribute for the field.
  15. * @var boolean $autofocus Is autofocus enabled?
  16. * @var string $class Classes for the input.
  17. * @var string $description Description of the field.
  18. * @var boolean $disabled Is this field disabled?
  19. * @var string $group Group the field belongs to. <fields> section in form XML.
  20. * @var boolean $hidden Is this field hidden in the form?
  21. * @var string $hint Placeholder for the field.
  22. * @var string $id DOM id of the field.
  23. * @var string $label Label of the field.
  24. * @var string $labelclass Classes to apply to the label.
  25. * @var boolean $multiple Does this field support multiple values?
  26. * @var string $name Name of the input field.
  27. * @var string $onchange Onchange attribute for the field.
  28. * @var string $onclick Onclick attribute for the field.
  29. * @var string $pattern Pattern (Reg Ex) of value of the form field.
  30. * @var boolean $readonly Is this field read only?
  31. * @var boolean $repeat Allows extensions to duplicate elements.
  32. * @var boolean $required Is this field required?
  33. * @var integer $size Size attribute of the input.
  34. * @var boolean $spellcheck Spellcheck state for the form field.
  35. * @var string $validate Validation rules to apply.
  36. * @var string $value Value attribute of the field.
  37. * @var array $options Options available for this field.
  38. * @var string $dataAttribute Miscellaneous data attributes preprocessed for HTML output
  39. * @var array $dataAttributes Miscellaneous data attributes for eg, data-*.
  40. */
  41. $alt = preg_replace('/[^a-zA-Z0-9_\-]/', '_', $name);
  42. $isBtnGroup = strpos(trim($class), 'btn-group') !== false;
  43. $isBtnYesNo = strpos(trim($class), 'btn-group-yesno') !== false;
  44. $classToggle = $isBtnGroup ? 'btn-check' : 'form-check-input';
  45. $btnClass = $isBtnGroup ? 'btn btn-outline-secondary' : 'form-check-label';
  46. $blockStart = $isBtnGroup ? '' : '<div class="form-check">';
  47. $blockEnd = $isBtnGroup ? '' : '</div>';
  48. // Add the attributes of the fieldset in an array
  49. $containerClass = trim($class . ' radio' . ($readonly || $disabled ? ' disabled' : '') . ($readonly ? ' readonly' : ''));
  50. $attribs = ['id="' . $id . '"'];
  51. if (!empty($disabled)) {
  52. $attribs[] = 'disabled';
  53. }
  54. if (!empty($autofocus)) {
  55. $attribs[] = 'autofocus';
  56. }
  57. if ($readonly || $disabled) {
  58. $attribs[] = 'style="pointer-events: none"';
  59. }
  60. if ($dataAttribute) {
  61. $attribs[] = $dataAttribute;
  62. }
  63. ?>
  64. <fieldset <?php echo implode(' ', $attribs); ?>>
  65. <legend class="visually-hidden">
  66. <?php echo $label; ?>
  67. </legend>
  68. <div class="<?php echo $containerClass; ?>">
  69. <?php foreach ($options as $i => $option) : ?>
  70. <?php echo $blockStart; ?>
  71. <?php
  72. $disabled = !empty($option->disable) ? 'disabled' : '';
  73. $style = $disabled ? ' style="pointer-events: none"' : '';
  74. // Initialize some option attributes.
  75. if ($isBtnYesNo) {
  76. // Set the button classes for the yes/no group
  77. switch ($option->value) {
  78. case '0':
  79. $btnClass = 'btn btn-outline-danger';
  80. break;
  81. case '1':
  82. $btnClass = 'btn btn-outline-success';
  83. break;
  84. default:
  85. $btnClass = 'btn btn-outline-secondary';
  86. break;
  87. }
  88. }
  89. $optionClass = !empty($option->class) ? $option->class : $btnClass;
  90. $optionClass = trim($optionClass . ' ' . $disabled);
  91. $checked = ((string) $option->value === $value) ? 'checked="checked"' : '';
  92. // Initialize some JavaScript option attributes.
  93. $onclick = !empty($option->onclick) ? 'onclick="' . $option->onclick . '"' : '';
  94. $onchange = !empty($option->onchange) ? 'onchange="' . $option->onchange . '"' : '';
  95. $oid = $id . $i;
  96. $ovalue = htmlspecialchars($option->value, ENT_COMPAT, 'UTF-8');
  97. $attributes = array_filter(array($checked, $disabled, ltrim($style), $onchange, $onclick));
  98. ?>
  99. <?php if ($required) : ?>
  100. <?php $attributes[] = 'required'; ?>
  101. <?php endif; ?>
  102. <input class="<?php echo $classToggle; ?>" type="radio" id="<?php echo $oid; ?>" name="<?php echo $name; ?>" value="<?php echo $ovalue; ?>" <?php echo implode(' ', $attributes); ?>>
  103. <label for="<?php echo $oid; ?>" class="<?php echo trim($optionClass); ?>"<?php echo $style; ?>>
  104. <?php echo $option->text; ?>
  105. </label>
  106. <?php echo $blockEnd; ?>
  107. <?php endforeach; ?>
  108. </div>
  109. </fieldset>