Skip to content

Commit

Permalink
Make it possible to use value form elements
Browse files Browse the repository at this point in the history
  • Loading branch information
Dominic Tubach committed Mar 13, 2024
1 parent cf54b15 commit a5c5899
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/Form/Control/Callbacks/ValueElementValueCallback.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

/*
* Copyright (C) 2022 SYSTOPIA GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

declare(strict_types=1);

namespace Drupal\json_forms\Form\Control\Callbacks;

use Drupal\Core\Form\FormStateInterface;

final class ValueElementValueCallback {

/**
* @param array<int|string, mixed> $element
* @param mixed $input
* @param \Drupal\Core\Form\FormStateInterface $formState
*
* @return mixed
*/
public static function convert(array $element, $input, FormStateInterface $formState) {
$value = $element['#value'] ?? $element['#default_value'] ?? NULL;
if (NULL === $value) {
// Prevent empty string as value. Drupal sets an empty string in this
// case if no value is set in the form state.
$formState->setValueForElement($element, NULL);
}

return $value;
}

}
61 changes: 61 additions & 0 deletions src/Form/Control/ValueArrayFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

/*
* Copyright (C) 2022 SYSTOPIA GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

declare(strict_types=1);

namespace Drupal\json_forms\Form\Control;

use Assert\Assertion;
use Drupal\Core\Form\FormStateInterface;
use Drupal\json_forms\Form\AbstractConcreteFormArrayFactory;
use Drupal\json_forms\Form\Control\Callbacks\ValueElementValueCallback;
use Drupal\json_forms\Form\Control\Util\BasicFormPropertiesFactory;
use Drupal\json_forms\Form\FormArrayFactoryInterface;
use Drupal\json_forms\JsonForms\Definition\Control\ControlDefinition;
use Drupal\json_forms\JsonForms\Definition\DefinitionInterface;

class ValueArrayFactory extends AbstractConcreteFormArrayFactory {

public static function getPriority(): int {
return HiddenArrayFactory::getPriority() + 1;
}

/**
* {@inheritDoc}
*/
public function createFormArray(DefinitionInterface $definition,
FormStateInterface $formState,
FormArrayFactoryInterface $formArrayFactory
): array {
Assertion::isInstanceOf($definition, ControlDefinition::class);
/** @var \Drupal\json_forms\JsonForms\Definition\Control\ControlDefinition $definition */

return [
'#type' => 'value',
'#value_callback' => ValueElementValueCallback::class . '::convert',
] + BasicFormPropertiesFactory::createFieldProperties($definition, $formState);
}

public function supportsDefinition(DefinitionInterface $definition): bool {
return $definition instanceof ControlDefinition
&& 'hidden' === $definition->getOptionsValue('type')
&& TRUE === $definition->getOptionsValue('internal');
}

}

0 comments on commit a5c5899

Please sign in to comment.