class Checkbox
Same name in other branches
- 9 core/lib/Drupal/Core/Render/Element/Checkbox.php \Drupal\Core\Render\Element\Checkbox
- 10 core/lib/Drupal/Core/Render/Element/Checkbox.php \Drupal\Core\Render\Element\Checkbox
- 11.x core/lib/Drupal/Core/Render/Element/Checkbox.php \Drupal\Core\Render\Element\Checkbox
Provides a form element for a single checkbox.
Properties:
- #return_value: The value to return when the checkbox is checked.
Usage example:
$form['copy'] = array(
'#type' => 'checkbox',
'#title' => $this->t('Send me a copy'),
);
Plugin annotation
@FormElement("checkbox");
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements \Drupal\Component\Plugin\PluginInspectionInterface, \Drupal\Component\Plugin\DerivativeInspectionInterface
- class \Drupal\Core\Plugin\PluginBase extends \Drupal\Component\Plugin\PluginBase uses \Drupal\Core\StringTranslation\StringTranslationTrait, \Drupal\Core\DependencyInjection\DependencySerializationTrait, \Drupal\Core\Messenger\MessengerTrait
- class \Drupal\Core\Render\Element\RenderElement extends \Drupal\Core\Plugin\PluginBase implements \Drupal\Core\Render\Element\ElementInterface
- class \Drupal\Core\Render\Element\FormElement extends \Drupal\Core\Render\Element\RenderElement implements \Drupal\Core\Render\Element\FormElementInterface
- class \Drupal\Core\Render\Element\Checkbox extends \Drupal\Core\Render\Element\FormElement
- class \Drupal\Core\Render\Element\FormElement extends \Drupal\Core\Render\Element\RenderElement implements \Drupal\Core\Render\Element\FormElementInterface
- class \Drupal\Core\Render\Element\RenderElement extends \Drupal\Core\Plugin\PluginBase implements \Drupal\Core\Render\Element\ElementInterface
- class \Drupal\Core\Plugin\PluginBase extends \Drupal\Component\Plugin\PluginBase uses \Drupal\Core\StringTranslation\StringTranslationTrait, \Drupal\Core\DependencyInjection\DependencySerializationTrait, \Drupal\Core\Messenger\MessengerTrait
Expanded class hierarchy of Checkbox
See also
\Drupal\Core\Render\Element\Checkboxes
21 string references to 'Checkbox'
- AssertLegacyTrait::assertFieldsByValue in core/
tests/ Drupal/ FunctionalTests/ AssertLegacyTrait.php - Asserts that a field exists in the current page with a given Xpath result.
- Callbacks::checkboxCallback in core/
modules/ system/ tests/ modules/ ajax_forms_test/ src/ Callbacks.php - Ajax callback triggered by checkbox.
- Checkbox::preRenderCheckbox in core/
lib/ Drupal/ Core/ Render/ Element/ Checkbox.php - Prepares a #type 'checkbox' render element for input.html.twig.
- claro_preprocess_input in core/
themes/ claro/ claro.theme - Implements template_preprocess_HOOK() for input.
- ContentTranslationHandler::addTranslatabilityClue in core/
modules/ content_translation/ src/ ContentTranslationHandler.php - Adds a clue about the form element translatability.
198 #type uses of Checkbox
- AccountForm::form in core/
modules/ user/ src/ AccountForm.php - Gets the actual form array to be built.
- AccountSettingsForm::buildForm in core/
modules/ user/ src/ AccountSettingsForm.php - Form constructor.
- ActionAjaxTest::buildConfigurationForm in core/
modules/ action/ tests/ action_form_ajax_test/ src/ Plugin/ Action/ ActionAjaxTest.php - Form constructor.
- AddFormBase::buildSelectedItemElement in core/
modules/ media_library/ src/ Form/ AddFormBase.php - Returns a render array for a single pre-selected media item.
- AdvancedSettingsForm::buildForm in core/
modules/ views_ui/ src/ Form/ AdvancedSettingsForm.php - Form constructor.
File
-
core/
lib/ Drupal/ Core/ Render/ Element/ Checkbox.php, line 26
Namespace
Drupal\Core\Render\ElementView source
class Checkbox extends FormElement {
/**
* {@inheritdoc}
*/
public function getInfo() {
$class = get_class($this);
return [
'#input' => TRUE,
'#return_value' => 1,
'#process' => [
[
$class,
'processCheckbox',
],
[
$class,
'processAjaxForm',
],
[
$class,
'processGroup',
],
],
'#pre_render' => [
[
$class,
'preRenderCheckbox',
],
[
$class,
'preRenderGroup',
],
],
'#theme' => 'input__checkbox',
'#theme_wrappers' => [
'form_element',
],
'#title_display' => 'after',
];
}
/**
* {@inheritdoc}
*/
public static function valueCallback(&$element, $input, FormStateInterface $form_state) {
if ($input === FALSE) {
// Use #default_value as the default value of a checkbox, except change
// NULL to 0, because FormBuilder::handleInputElement() would otherwise
// replace NULL with empty string, but an empty string is a potentially
// valid value for a checked checkbox.
return isset($element['#default_value']) ? $element['#default_value'] : 0;
}
else {
// Checked checkboxes are submitted with a value (possibly '0' or ''):
// http://www.w3.org/TR/html401/interact/forms.html#successful-controls.
// For checked checkboxes, browsers submit the string version of
// #return_value, but we return the original #return_value. For unchecked
// checkboxes, browsers submit nothing at all, but
// FormBuilder::handleInputElement() detects this, and calls this
// function with $input=NULL. Returning NULL from a value callback means
// to use the default value, which is not what is wanted when an unchecked
// checkbox is submitted, so we use integer 0 as the value indicating an
// unchecked checkbox. Therefore, modules must not use integer 0 as a
// #return_value, as doing so results in the checkbox always being treated
// as unchecked. The string '0' is allowed for #return_value. The most
// common use-case for setting #return_value to either 0 or '0' is for the
// first option within a 0-indexed array of checkboxes, and for this,
// \Drupal\Core\Render\Element\Checkboxes::processCheckboxes() uses the
// string rather than the integer.
return isset($input) ? $element['#return_value'] : 0;
}
}
/**
* Prepares a #type 'checkbox' render element for input.html.twig.
*
* @param array $element
* An associative array containing the properties of the element.
* Properties used: #title, #value, #return_value, #description, #required,
* #attributes, #checked.
*
* @return array
* The $element with prepared variables ready for input.html.twig.
*/
public static function preRenderCheckbox($element) {
$element['#attributes']['type'] = 'checkbox';
Element::setAttributes($element, [
'id',
'name',
'#return_value' => 'value',
]);
// Unchecked checkbox has #value of integer 0.
if (!empty($element['#checked'])) {
$element['#attributes']['checked'] = 'checked';
}
static::setAttributes($element, [
'form-checkbox',
]);
return $element;
}
/**
* Sets the #checked property of a checkbox element.
*/
public static function processCheckbox(&$element, FormStateInterface $form_state, &$complete_form) {
$value = $element['#value'];
$return_value = $element['#return_value'];
// On form submission, the #value of an available and enabled checked
// checkbox is #return_value, and the #value of an available and enabled
// unchecked checkbox is integer 0. On not submitted forms, and for
// checkboxes with #access=FALSE or #disabled=TRUE, the #value is
// #default_value (integer 0 if #default_value is NULL). Most of the time,
// a string comparison of #value and #return_value is sufficient for
// determining the "checked" state, but a value of TRUE always means checked
// (even if #return_value is 'foo'), and a value of FALSE or integer 0
// always means unchecked (even if #return_value is '' or '0').
if ($value === TRUE || $value === FALSE || $value === 0) {
$element['#checked'] = (bool) $value;
}
else {
// Compare as strings, so that 15 is not considered equal to '15foo', but
// 1 is considered equal to '1'. This cast does not imply that either
// #value or #return_value is expected to be a string.
$element['#checked'] = (string) $value === (string) $return_value;
}
return $element;
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overriden Title | Overrides |
---|---|---|---|---|---|
Checkbox::getInfo | public | function | Returns the element properties for this element. | Overrides ElementInterface::getInfo | |
Checkbox::preRenderCheckbox | public static | function | Prepares a #type 'checkbox' render element for input.html.twig. | ||
Checkbox::processCheckbox | public static | function | Sets the #checked property of a checkbox element. | ||
Checkbox::valueCallback | public static | function | Determines how user input is mapped to an element's #value property. | Overrides FormElement::valueCallback | |
FormElement::processAutocomplete | public static | function | Adds autocomplete functionality to elements. | ||
FormElement::processPattern | public static | function | #process callback for #pattern form element property. | ||
FormElement::validatePattern | public static | function | #element_validate callback for #pattern form element property. | ||
PluginInspectionInterface::getPluginDefinition | public | function | Gets the definition of the plugin implementation. | 6 | |
PluginInspectionInterface::getPluginId | public | function | Gets the plugin_id of the plugin instance. | 2 | |
RenderElement::preRenderAjaxForm | public static | function | Adds Ajax information about an element to communicate with JavaScript. | ||
RenderElement::preRenderGroup | public static | function | Adds members of this group as actual elements for rendering. | ||
RenderElement::processAjaxForm | public static | function | Form element processing handler for the #ajax form property. | 1 | |
RenderElement::processGroup | public static | function | Arranges elements into groups. | ||
RenderElement::setAttributes | public static | function | Sets a form element's class attribute. | Overrides ElementInterface::setAttributes |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.