Skip to content

Commit

Permalink
Add support for read only fields
Browse files Browse the repository at this point in the history
  • Loading branch information
Dominic Tubach committed Jul 26, 2024
1 parent c82eb20 commit da2a8d1
Show file tree
Hide file tree
Showing 12 changed files with 352 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
use Civi\RemoteTools\EntityProfile\Helper\ProfileEntityLoaderInterface;
use Civi\RemoteTools\EntityProfile\RemoteEntityProfileInterface;
use Civi\RemoteTools\Form\FormSpec\FormSpec;
use Civi\RemoteTools\Form\FormSpec\Util\ReadOnlyFieldsRemover;
use Civi\RemoteTools\Form\Validation\ValidationResult;
use CRM_Remotetools_ExtensionUtil as E;

Expand Down Expand Up @@ -219,7 +220,7 @@ public function submitCreateForm(RemoteSubmitCreateFormAction $action
$createdValues = $this->api4->createEntity(
$this->profile->getEntityName(),
$formSpec->getDataTransformer()->toEntityValues(
$action->getData(),
ReadOnlyFieldsRemover::removeReadOnlyFields($formSpec, $action->getData()),
NULL,
$action->getResolvedContactId()
),
Expand Down Expand Up @@ -259,7 +260,7 @@ public function submitUpdateForm(RemoteSubmitUpdateFormAction $action
}

$newEntityValues = $formSpec->getDataTransformer()->toEntityValues(
$action->getData(),
ReadOnlyFieldsRemover::removeReadOnlyFields($formSpec, $action->getData()),
$entityValues,
$action->getResolvedContactId()
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
/**
* @api
*/
final class RemoteSubmitCreateFormAction extends AbstractProfileAwareRemoteAction {
class RemoteSubmitCreateFormAction extends AbstractProfileAwareRemoteAction {

use ArgumentsParameterOptionalTrait;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
/**
* @api
*/
final class RemoteSubmitUpdateFormAction extends AbstractProfileAwareRemoteAction {
class RemoteSubmitUpdateFormAction extends AbstractProfileAwareRemoteAction {

use DataParameterTrait;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
/**
* @api
*/
final class RemoteValidateUpdateFormAction extends AbstractProfileAwareRemoteAction {
class RemoteValidateUpdateFormAction extends AbstractProfileAwareRemoteAction {

use DataParameterTrait;

Expand Down
15 changes: 15 additions & 0 deletions Civi/RemoteTools/Form/FormSpec/AbstractFormField.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ abstract class AbstractFormField extends AbstractFormInput {

private bool $required = FALSE;

private bool $readOnly = FALSE;

private ?bool $nullable = NULL;

private bool $hasDefaultValue = FALSE;
Expand Down Expand Up @@ -41,6 +43,19 @@ public function setRequired(bool $required): self {
return $this;
}

public function isReadOnly(): bool {
return $this->readOnly;
}

/**
* @return $this
*/
public function setReadOnly(bool $readOnly): self {
$this->readOnly = $readOnly;

return $this;
}

public function isNullable(): bool {
return NULL === $this->nullable ? !$this->isRequired() : $this->nullable;
}
Expand Down
25 changes: 25 additions & 0 deletions Civi/RemoteTools/Form/FormSpec/Util/ReadOnlyFieldsRemover.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
declare(strict_types = 1);

namespace Civi\RemoteTools\Form\FormSpec\Util;

use Civi\RemoteTools\Form\FormSpec\FormSpec;

final class ReadOnlyFieldsRemover {

/**
* @phpstan-param array<string, mixed> $values
*
* @phpstan-return array<string, mixed>
*/
public static function removeReadOnlyFields(FormSpec $formSpec, array $values): array {
foreach ($formSpec->getFields() as $field) {
if ($field->isReadOnly()) {
unset($values[$field->getName()]);
}
}

return $values;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ public function createSchema(AbstractFormField $field): JsonSchema {
if ($field->hasDefaultValue()) {
$keywords['default'] = $field->getDefaultValue();
}
if ($field->isReadOnly()) {
$keywords['readOnly'] = TRUE;
}

return new JsonSchemaBoolean($keywords, $field->isNullable());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ public function createSchema(AbstractFormField $field): JsonSchema {
if ($field->hasDefaultValue()) {
$keywords['default'] = $field->getDefaultValue();
}
if ($field->isReadOnly()) {
$keywords['readOnly'] = TRUE;
}
if ($field instanceof AbstractNumberField) {
if (NULL !== $field->getMaximum()) {
$keywords['maximum'] = $field->getMaximum();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ public function createSchema(AbstractFormField $field): JsonSchema {
if ($field->hasDefaultValue()) {
$keywords['default'] = $field->getDefaultValue();
}
if ($field->isReadOnly()) {
$keywords['readOnly'] = TRUE;
}
if ($field instanceof AbstractNumberField) {
if (NULL !== $field->getMaximum()) {
$keywords['maximum'] = $field->getMaximum();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ public function createSchema(AbstractFormField $field): JsonSchema {
if ($field->hasDefaultValue()) {
$keywords['default'] = $field->getDefaultValue();
}
if ($field->isReadOnly()) {
$keywords['readOnly'] = TRUE;
}

return new JsonSchema($keywords);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ public function createSchema(AbstractFormField $field): JsonSchema {
if ($field->hasDefaultValue()) {
$keywords['default'] = $field->getDefaultValue();
}
if ($field->isReadOnly()) {
$keywords['readOnly'] = TRUE;
}
if ($field instanceof AbstractTextField) {
if (NULL !== $field->getMaxLength()) {
$keywords['maxLength'] = $field->getMaxLength();
Expand Down
Loading

0 comments on commit da2a8d1

Please sign in to comment.