CheckboxSettingType.php in UI Patterns Settings 8
File
src/Plugin/UiPatterns/SettingType/CheckboxSettingType.php
View source
<?php
namespace Drupal\ui_patterns_settings\Plugin\UIPatterns\SettingType;
use Drupal\ui_patterns_settings\Definition\PatternDefinitionSetting;
use Drupal\ui_patterns_settings\Plugin\PatternSettingTypeBase;
class CheckboxSettingType extends PatternSettingTypeBase {
public function settingsPreprocess($value, array $context, PatternDefinitionSetting $def) {
$selected_options = [];
$defined_options = $def
->getOptions();
if (is_array($value)) {
foreach ($value as $checkbox_key => $checkbox_value) {
if ($checkbox_value != "0") {
$selected_options[$checkbox_key] = isset($defined_options[$checkbox_value]) ? $defined_options[$checkbox_value] : $checkbox_value;
}
}
}
return $selected_options;
}
public function settingsForm(array $form, $value, PatternDefinitionSetting $def) {
$def = $this
->getPatternSettingDefinition();
$value = $this
->getValue($value);
if (is_scalar($value)) {
$value = [
$value,
];
}
if (empty($value)) {
$value = [];
}
$form[$def
->getName()] = [
'#type' => 'checkboxes',
'#title' => $def
->getLabel(),
'#description' => $def
->getDescription(),
'#default_value' => $value,
'#required' => $def
->getRequired(),
'#options' => $def
->getOptions(),
];
return $form;
}
}