Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it possible to use an empty string in a select field #78

Merged
merged 1 commit into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Form/Control/Callbacks/SelectCallbacks.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public static function validate(array $element, FormStateInterface $formState):
*/
public static function value(array &$element, $input, FormStateInterface $formState) {
if (array_key_exists('#empty_value', $element) && $input === (string) $element['#empty_value']) {
$input = '' === $element['#empty_value'] ? NULL : $element['#empty_value'];
$input = $element['#_empty_value'];

if (NULL === $input) {
// Prevent empty string as value. Drupal sets an empty string in this
Expand Down
4 changes: 2 additions & 2 deletions src/Form/Control/CheckboxesArrayFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
use Drupal\json_forms\Form\AbstractConcreteFormArrayFactory;
use Drupal\json_forms\Form\Control\Callbacks\CheckboxesCallbacks;
use Drupal\json_forms\Form\Control\Util\BasicFormPropertiesFactory;
use Drupal\json_forms\Form\Control\Util\OptionsBuilder;
use Drupal\json_forms\Form\Control\Util\OptionsUtil;
use Drupal\json_forms\Form\FormArrayFactoryInterface;
use Drupal\json_forms\Form\Util\FormCallbackRegistrator;
use Drupal\json_forms\JsonForms\Definition\Control\ArrayControlDefinition;
Expand Down Expand Up @@ -54,7 +54,7 @@ public function createFormArray(
$form = [
'#type' => 'checkboxes',
// @todo Handle non-string values and integerish strings.
'#options' => OptionsBuilder::buildOptions($definition),
'#options' => OptionsUtil::buildOptions($definition),
] + BasicFormPropertiesFactory::createFieldProperties($definition, $formState);

if (isset($form['#default_value'])) {
Expand Down
6 changes: 3 additions & 3 deletions src/Form/Control/RadiosArrayFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
use Drupal\json_forms\Form\AbstractConcreteFormArrayFactory;
use Drupal\json_forms\Form\Control\Callbacks\OptionValueCallbacks;
use Drupal\json_forms\Form\Control\Util\BasicFormPropertiesFactory;
use Drupal\json_forms\Form\Control\Util\OptionsBuilder;
use Drupal\json_forms\Form\Control\Util\OptionsUtil;
use Drupal\json_forms\Form\FormArrayFactoryInterface;
use Drupal\json_forms\JsonForms\Definition\Control\ControlDefinition;
use Drupal\json_forms\JsonForms\Definition\DefinitionInterface;
Expand All @@ -49,8 +49,8 @@ public function createFormArray(
/** @var \Drupal\json_forms\JsonForms\Definition\Control\ControlDefinition $definition */
$form = [
'#type' => 'radios',
'#options' => OptionsBuilder::buildOptions($definition),
'#_option_values' => OptionsBuilder::buildOptionValues($definition),
'#options' => OptionsUtil::buildOptions($definition),
'#_option_values' => OptionsUtil::buildOptionValues($definition),
'#value_callback' => OptionValueCallbacks::class . '::value',
'#element_validate' => [OptionValueCallbacks::class . '::validate'],
'#_type' => $definition->getType(),
Expand Down
13 changes: 10 additions & 3 deletions src/Form/Control/SelectArrayFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
use Drupal\json_forms\Form\AbstractConcreteFormArrayFactory;
use Drupal\json_forms\Form\Control\Callbacks\SelectCallbacks;
use Drupal\json_forms\Form\Control\Util\BasicFormPropertiesFactory;
use Drupal\json_forms\Form\Control\Util\OptionsBuilder;
use Drupal\json_forms\Form\Control\Util\OptionsUtil;
use Drupal\json_forms\Form\FormArrayFactoryInterface;
use Drupal\json_forms\JsonForms\Definition\Control\ControlDefinition;
use Drupal\json_forms\JsonForms\Definition\DefinitionInterface;
Expand All @@ -49,15 +49,22 @@ public function createFormArray(
/** @var \Drupal\json_forms\JsonForms\Definition\Control\ControlDefinition $definition */
$form = [
'#type' => 'select',
'#options' => OptionsBuilder::buildOptions($definition),
'#_option_values' => OptionsBuilder::buildOptionValues($definition),
'#options' => OptionsUtil::buildOptions($definition),
'#_option_values' => OptionsUtil::buildOptionValues($definition),
'#value_callback' => SelectCallbacks::class . '::value',
'#element_validate' => [SelectCallbacks::class . '::validate'],
] + BasicFormPropertiesFactory::createFieldProperties($definition, $formState);

// #empty_value cannot be NULL, thus we use #_empty_value to allow NULL as
// well as an empty string.
$form['#_empty_value'] = OptionsUtil::getEmptyOptionValue($definition);
if (!$definition->isRequired()) {
$form['#empty_value'] = '';
}
// @phpstan-ignore offsetAccess.nonOffsetAccessible
if (isset($form['#options']['']) && '' !== $form['#options']['']) {
$form['#empty_option'] = $form['#options'][''];
}

return $form;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
use Drupal\json_forms\JsonForms\Definition\Control\ArrayControlDefinition;
use Drupal\json_forms\JsonForms\Definition\Control\ControlDefinition;

final class OptionsBuilder {
final class OptionsUtil {

/**
* @param \Drupal\json_forms\JsonForms\Definition\Control\ControlDefinition $definition
Expand Down Expand Up @@ -73,6 +73,28 @@ public static function buildOptionValues(ControlDefinition $definition): array {
return $optionValues;
}

/**
* @return ''|null
* The actual value to use if the option with the empty value is selected.
*/
public static function getEmptyOptionValue(ControlDefinition $definition): ?string {
$enums = self::getEnum($definition);
if (in_array('', $enums, TRUE)) {
return '';
}
if (in_array(NULL, $enums, TRUE)) {
return NULL;
}

foreach (self::getOneOf($definition) as $option) {
if (\property_exists($option, 'const') && (NULL === $option->const || '' === $option->const)) {
return $option->const;
}
}

return NULL;
}

/**
* @phpstan-return array<scalar|null>
*/
Expand Down
Loading