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

#4306: properties of schema array/items do not respect property specs regarding element type #4307

Open
wants to merge 3 commits into
base: 2.x
Choose a base branch
from
Open
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 modules/json_form_widget/json_form_widget.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ services:
class: \Drupal\json_form_widget\ObjectHelper
json_form.array_helper:
class: \Drupal\json_form_widget\ArrayHelper
arguments: ['@json_form.object_helper']
arguments: ['@json_form.object_helper', '@json_form.string_helper']
json_form.widget_router:
class: \Drupal\json_form_widget\WidgetRouter
arguments: ['@uuid','@json_form.string_helper', '@dkan.metastore.service']
Expand Down
16 changes: 13 additions & 3 deletions modules/json_form_widget/src/ArrayHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ class ArrayHelper implements ContainerInjectionInterface {
*/
protected ObjectHelper $objectHelper;


/**
* String Helper.
*
* @var StringHelper
*/
protected StringHelper $stringHelper;

/**
* Builder object.
*
Expand All @@ -36,15 +44,17 @@ class ArrayHelper implements ContainerInjectionInterface {
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('json_form.object_helper')
$container->get('json_form.object_helper'),
$container->get('json_form.string_helper')
);
}

/**
* Constructor.
*/
public function __construct(ObjectHelper $object_helper) {
public function __construct(ObjectHelper $object_helper, StringHelper $string_helper) {
$this->objectHelper = $object_helper;
$this->stringHelper = $string_helper;
}

/**
Expand Down Expand Up @@ -264,7 +274,7 @@ protected function buildArrayElement(array $definition, $data, FormStateInterfac
*/
protected function buildSimpleArrayElement(array $definition, $data): array {
return array_filter([
'#type' => 'textfield',
'#type' => $this->stringHelper->getElementType($definition['schema']->items),
'#title' => $definition['schema']->items->title ?? NULL,
'#default_value' => $data,
]);
Expand Down
6 changes: 5 additions & 1 deletion modules/json_form_widget/tests/src/Unit/ArrayHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ public function testComplex() {

$options = (new Options())
->add('json_form.object_helper', ObjectHelper::class)
->add('json_form.string_helper', StringHelper::class)
->index(0);
$chain = (new Chain($this))
->add(Container::class, 'get', $options)
->add(ObjectHelper::class, 'handleObjectElement', $object)
->add(StringHelper::class, 'handleStringElement', $object)
->getMock();

$array_helper = ArrayHelper::create($chain);
Expand All @@ -59,7 +61,9 @@ public function testComplex() {
->add(SchemaRetriever::class, 'retrieve', $distribution_schema)
->add(SchemaUiHandler::class, 'setSchemaUi')
->add(ObjectHelper::class, 'handleObjectElement', $object)
->add(ObjectHelper::class, 'setBuilder');
->add(ObjectHelper::class, 'setBuilder')
->add(StringHelper::class, 'handleStringElement', $object)
->add(StringHelper::class, 'setBuilder');

$container = $container_chain->getMock();
\Drupal::setContainer($container);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ private function getRouter() {
$email_validator = new EmailValidator();
$string_helper = new StringHelper($email_validator);
$object_helper = new ObjectHelper();
$array_helper = new ArrayHelper($object_helper);
$array_helper = new ArrayHelper($object_helper, $string_helper);
$integer_helper = new IntegerHelper();

$options = (new Options())
Expand Down