Skip to content

Adjusted UI form XML to be valid and accessible via the admin #313

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

Merged
merged 4 commits into from
Oct 21, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<item name="provider" xsi:type="string">${NAME}.${NAME}_data_source</item>
</item>
<item name="label" xsi:type="string" translate="true">${LABEL}</item>
<item name="template" xsi:type="string">templates/form/collapsible</item>
</argument>
<settings>
<namespace>${NAME}</namespace>
Expand All @@ -20,6 +21,11 @@
<settings>
<submitUrl path="${ROUTE}/${SUBMIT_CONTROLLER}/${SUBMIT_ACTION}"/>
</settings>
<dataProvider class="${DATA_PROVIDER}" name="${NAME}_data_source"/>
<dataProvider class="${DATA_PROVIDER}" name="${NAME}_data_source">
<settings>
<requestFieldName>${PRIMARY_FIELD}</requestFieldName>
<primaryFieldName>${PRIMARY_FIELD}</primaryFieldName>
</settings>
</dataProvider>
</dataSource>
</form>
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,23 @@

public class UiComponentFormFieldsetData {

private final String name;
private final String label;
private final String sortOrder;

/**
* Fieldset data.
*
* @param name String
* @param label String
* @param sortOrder String
*/
public UiComponentFormFieldsetData(
final String name,
final String label,
final String sortOrder
) {
this.name = name;
this.label = label;
this.sortOrder = sortOrder;
}
Expand All @@ -31,4 +35,8 @@ public String getLabel() {
public String getSortOrder() {
return sortOrder;
}

public String getName() {
return name;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
<grid row="2" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<font style="1"/>
<text value="Buttons"/>
</properties>
</component>
Expand Down Expand Up @@ -106,6 +107,7 @@
<grid row="5" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<font style="1"/>
<text value="Fieldsets"/>
</properties>
</component>
Expand All @@ -114,6 +116,7 @@
<grid row="8" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<font style="1"/>
<text value="Fields"/>
</properties>
</component>
Expand Down Expand Up @@ -208,6 +211,7 @@
</grid>
</constraints>
<properties>
<font style="1"/>
<text value="Submit Controller"/>
</properties>
</component>
Expand Down Expand Up @@ -242,6 +246,7 @@
<grid row="0" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<font style="1"/>
<text value="View Controller"/>
</properties>
</component>
Expand Down Expand Up @@ -278,6 +283,7 @@
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<font style="1"/>
<text value="Data Provider"/>
</properties>
</component>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,8 @@ protected void initButtonsTable() {
protected void initFieldSetsTable() {
final DefaultTableModel model = getFieldsetsModel();
model.setDataVector(
new Object[][] {{"General","10",DELETE_COLUMN}},
new Object[] { LABEL_COLUMN, SORT_ORDER_COLUMN, ACTION_COLUMN}
new Object[][] {{"general", "General","10",DELETE_COLUMN}},
new Object[] { NAME_COLUMN, LABEL_COLUMN, SORT_ORDER_COLUMN, ACTION_COLUMN}
);

final TableColumn column = fieldsets.getColumn(ACTION_COLUMN);
Expand Down Expand Up @@ -616,10 +616,12 @@ public List<UiComponentFormFieldsetData> getFieldsets() {
final ArrayList<UiComponentFormFieldsetData> fieldsets =
new ArrayList<>();
for (int count = 0; count < model.getRowCount(); count++) {
final String label = model.getValueAt(count, 0).toString();
final String sortOrder = model.getValueAt(count, 1).toString();
final String name = model.getValueAt(count, 0).toString();
final String label = model.getValueAt(count, 1).toString();
final String sortOrder = model.getValueAt(count, 2).toString();

final UiComponentFormFieldsetData fieldsetData = new UiComponentFormFieldsetData(//NOPMD
name,
label,
sortOrder
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ public boolean validate() {
while (fieldsetIterator.hasNext()) {
final UiComponentFormFieldsetData fieldset = fieldsetIterator.next();

final String name = fieldset.getName();
if (!NotEmptyRule.getInstance().check(name)) {
showErrorMessage(
validatorBundle.message(NotEmptyRule.MESSAGE, "Fieldset Name")
);
valid = false;
break;
}

final String label = fieldset.getLabel();
if (!NotEmptyRule.getInstance().check(label)) {
showErrorMessage(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import com.intellij.psi.xml.XmlFile;
import com.magento.idea.magento2plugin.actions.generation.data.UiComponentFormButtonData;
import com.magento.idea.magento2plugin.actions.generation.data.UiComponentFormFileData;
import com.magento.idea.magento2plugin.actions.generation.generator.code.ButtonsXmlDeclarationGenerator;
import com.magento.idea.magento2plugin.actions.generation.generator.code.XmlDeclarationsGenerator;
import com.magento.idea.magento2plugin.actions.generation.generator.util.DirectoryGenerator;
import com.magento.idea.magento2plugin.actions.generation.generator.util.FileFromTemplateGenerator;
import com.magento.idea.magento2plugin.indexes.ModuleIndex;
Expand Down Expand Up @@ -111,7 +111,7 @@ protected PsiFile createForm(
);
}

new ButtonsXmlDeclarationGenerator(uiFormFileData, project).generate(formFile);
new XmlDeclarationsGenerator(uiFormFileData, project).generate(formFile);

return formFile;
}
Expand All @@ -131,6 +131,7 @@ protected void fillAttributes(final Properties attributes) {
uiFormFileData.getSubmitActionName().toLowerCase(new Locale("en","EN"))
);
attributes.setProperty("DATA_PROVIDER", uiFormFileData.getDataProviderFqn());
attributes.setProperty("PRIMARY_FIELD", uiFormFileData.getFields().get(0).getName());
}

private Areas getArea(final String area) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import java.util.Properties;
import org.jetbrains.annotations.NotNull;

public class ButtonsXmlDeclarationGenerator {
public class XmlDeclarationsGenerator {
private final UiComponentFormFileData uiFormFileData;
private final Project project;
private final GetCodeTemplate getCodeTemplate;
Expand All @@ -28,7 +28,7 @@ public class ButtonsXmlDeclarationGenerator {
* @param uiFormFileData UiFormFileData
* @param project Project
*/
public ButtonsXmlDeclarationGenerator(
public XmlDeclarationsGenerator(
final @NotNull UiComponentFormFileData uiFormFileData,
final Project project
) {
Expand All @@ -38,7 +38,7 @@ public ButtonsXmlDeclarationGenerator(
}

/**
* Injects button declarations to file.
* Injects buttons and fields declarations to file.
*
* @param formFile XmlFile
*/
Expand Down Expand Up @@ -67,35 +67,7 @@ public void generate(final XmlFile formFile) {
buttonsTag.addSubTag(buttonTag, false);
}

for (final UiComponentFormFieldsetData formFieldsetData
: uiFormFileData.getFieldsets()) {
final StringBuffer fieldsStringBuffer = new StringBuffer();//NOPMD

for (final UiComponentFormFieldData formFieldData : uiFormFileData.getFields()) {
if (!formFieldData.getFieldset().equals(formFieldsetData.getLabel())) {
continue;
}
try {
fieldsStringBuffer.append(getCodeTemplate.execute(
UiComponentFormXml.FIELD_TEMPLATE,
fillAttributes(formFieldData)
)
);
} catch (IOException e) {
return;
}
}

final XmlTag fieldsetTag = rootTag.createChildTag(
"fieldset",
null,
fieldsStringBuffer.toString(),
false
);
fieldsetTag.setAttribute("label", formFieldsetData.getLabel());
fieldsetTag.setAttribute("sortOrder",formFieldsetData.getSortOrder());
rootTag.addSubTag(fieldsetTag, false);
}
renderFieldsets(rootTag);

psiDocumentManager.commitDocument(document);
});
Expand All @@ -111,4 +83,54 @@ private Properties fillAttributes(final UiComponentFormFieldData formFieldData)
attributes.setProperty("SORT_ORDER", formFieldData.getSortOrder());
return attributes;
}

protected void renderFieldsets(final XmlTag rootTag) {
for (final UiComponentFormFieldsetData formFieldsetData
: uiFormFileData.getFieldsets()) {
final StringBuffer fieldsStringBuffer = new StringBuffer();//NOPMD

for (final UiComponentFormFieldData formFieldData : uiFormFileData.getFields()) {
if (!formFieldData.getFieldset().equals(formFieldsetData.getName())) {
continue;
}
try {
fieldsStringBuffer.append(getCodeTemplate.execute(
UiComponentFormXml.FIELD_TEMPLATE,
fillAttributes(formFieldData)
)
);
} catch (IOException e) {
return;
}
}

final XmlTag fieldsetTag = rootTag.createChildTag(
"fieldset",
null,
fieldsStringBuffer.toString(),
false
);
fieldsetTag.setAttribute("name", formFieldsetData.getName());
fieldsetTag.setAttribute("sortOrder", formFieldsetData.getSortOrder());

final XmlTag settings = fieldsetTag.createChildTag(
"settings",
null,
"",
false
);

final XmlTag label = settings.createChildTag(
"label",
null,
formFieldsetData.getLabel(),
false
);
label.setAttribute("translate", "true");

settings.addSubTag(label, false);
fieldsetTag.addSubTag(settings, true);
rootTag.addSubTag(fieldsetTag, false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<item name="provider" xsi:type="string">my_form.my_form_data_source</item>
</item>
<item name="label" xsi:type="string" translate="true">My Form</item>
<item name="template" xsi:type="string">templates/form/collapsible</item>
</argument>
<settings>
<namespace>my_form</namespace>
Expand All @@ -23,34 +24,21 @@
<settings>
<submitUrl path="customroute/myentity/save"/>
</settings>
<dataProvider class="Foo/Bar/Ui/MyEntity/DataProvider" name="my_form_data_source"/>
</dataSource>
<fieldset label="General" sortOrder="10">
<field name="my_field" sortOrder="10" formElement="input">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="source" xsi:type="string">entity</item>
</item>
</argument>
<dataProvider class="Foo/Bar/Ui/MyEntity/DataProvider" name="my_form_data_source">
<settings>
<dataType>text</dataType>
<label translate="true">My Field</label>
<dataScope>my_field</dataScope>
<requestFieldName>my_field</requestFieldName>
<primaryFieldName>my_field</primaryFieldName>
</settings>
</field>
</dataProvider>
</dataSource>
<fieldset name="general" sortOrder="10">
<settings>
<label translate="true">General</label>
</settings>
</fieldset>
<fieldset label="Test Fieldset" sortOrder="20">
<field name="my_field_2" sortOrder="10" formElement="input">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="source" xsi:type="string">entity</item>
</item>
</argument>
<settings>
<dataType>text</dataType>
<label translate="true">My Field 2</label>
<dataScope>my_field_2</dataScope>
</settings>
</field>
<fieldset name="test_fieldset" sortOrder="20">
<settings>
<label translate="true">Test Fieldset</label>
</settings>
</fieldset>
</form>
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,12 @@ protected List getFields() {
protected List getFieldsets() {
final List fieldsets = new ArrayList();
fieldsets.add(new UiComponentFormFieldsetData(
"general",
"General",
"10"
));
fieldsets.add(new UiComponentFormFieldsetData(
"test_fieldset",
"Test Fieldset",
"20"
));
Expand Down