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

Move and remove elements from JSON arrays in form #4347

Open
wants to merge 10 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
100 changes: 100 additions & 0 deletions modules/json_form_widget/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
## High level

```mermaid
sequenceDiagram
participant FormBuilder
participant FieldTypeRouter
participant Element Handlers
participant SchemaUiHandler

loop each $property in $form
FormBuilder ->> FieldTypeRouter: getFormElement()
FieldTypeRouter ->> Element Handlers: handler functions<br />on helper classes
Note over FieldTypeRouter, Element Handlers: Initial build of property<br />Elements
Element Handlers ->> FormBuilder: Return default element for $property
end

FormBuilder ->> SchemaUiHandler: applySchemaUi()
Note over FormBuilder, SchemaUiHandler: Now apply SchemaUi to full $form
loop each $property
SchemaUiHandler ->> SchemaUiHandler: applyOnBaseField()
SchemaUiHandler ->> SchemaUiHandler: handlePropertySpec()
Note over SchemaUiHandler, SchemaUiHandler: See handlePropertySpec() <br />internals diagram
end
SchemaUiHandler ->> FormBuilder: Return $form with SchemaUi alterations

```

## The handlePropertySpec() method

As called from withing SchemaUiHandler::applyShemaUi

```mermaid
sequenceDiagram
participant handlePropertySpec


```

## The initial build

```mermaid
graph TD
getForm["FormBuilder::getJsonForm()"] --> eachProp["foreach $properties"]
eachProp --> getElement["FieldTypeRouter::getFormElement()"]
getElement --> switch[Switch $type]
switch --> object{object}

object -- true --> handleObject["ObjectHelper::handleObjectElement()"]
handleObject --> generateObject["ObjectHelper::generateObjectElement()"]
generateObject --> generateProperties["ObjectHelper::generateProperties()"]
generateProperties -- recursion --> eachProp


object -- false --> array{array}
array -- true --> handleArray["ArrayHelper::handleArrayElement()"]
handleArray --> complex{Items are objects?}
complex -- no --> buildSimple["ArrayHelper::buildSimpleArrayElement()"]
complex -- yes --> buildComplex["ArrayHelper::buildComplexArrayElement()"]
buildComplex --> handleObject

array -- false --> string["string"]
string -- true --> handleString["StringHelper::handleStringElement()"]
string -- false --> integer["integer"]
integer -- true --> handleInteger["IntegerHelper::handleIntegerElement()"]
switch --> eachProp
eachProp --> getForm
```

## Customizing widgets w/SchemaUI

```mermaid
flowchart-elk TD
getForm["FormBuilder::getJsonForm()"] --> applySchemaUi["SchemaUiHandler::applySchemaUi()"]
applySchemaUi --> eachProp2["foreach schemaUI property"]
eachProp2 --> applyOnBaseField["SchemaUiHandler::applyOnBaseField()"]
eachProp2 --> handlePropertySpec
subgraph s1["applyOnBaseField()"]
applyOnBaseField --> updateWidgets["SchemaUiHandler::updatewidgets()"]
updateWidgets --> disableFields["SchemaUiHandler::disableFields()"]
disableFields --> addPlaceholders["SchemaUiHandler::addPlaceholders()"]
addPlaceholders --> changeFieldDescriptions["SchemaUiHandler::changeFieldDescriptions()"]
changeFieldDescriptions --> changeFieldTitle["SchemaUiHandler::changeFieldTitle()"]
end
subgraph s2["handlePropertySpec"]
handlePropertySpec["SchemaUiHandler::handlePropertySpec()"] --> what{"what is it"}
what -- array --> eachArrayElement
eachArrayElement --> applyOnArrayFields

applyOnArrayFields --> eachArrayElementField
eachArrayElementField --> inSpec{"Does SchemaUI<br>contain config for<br>this field?"}
inSpec -- yes --> handlePropertySpec
inSpec -- no --> applyOnBaseFieldRec["SchemaUiHandler::applyOnBaseField()"]

what -- object --> applyOnObjectFields
applyOnObjectFields --> eachObjField["foreach object property in the SchemaUi spec"]
eachObjField --> applyOnBaseFieldRec

end

```
2 changes: 1 addition & 1 deletion modules/json_form_widget/json_form_widget.module
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function json_form_widget_remove_one(array &$form, FormStateInterface $form_stat
/**
* Update count property by the given offset.
*
* @param FormStateInterface $form_state
* @param \Drupal\Core\Form\FormStateInterface $form_state
* Form state.
* @param int $offset
* Offset to change count by.
Expand Down
Loading