Skip to content

Custom field is not appearing in the form #4

@rajeev-k-tomy

Description

@rajeev-k-tomy

Problem

In the form layout xml file, I have a custom field defined like this:

<item name="store_ids" xsi:type="array">
    <item name="sort_order" xsi:type="number">70</item>
    <item name="label" xsi:type="string" translate="true">Stores</item>
    <item name="field_type" xsi:type="string">select</item>
    <item name="options" xsi:type="string">Magento\Store\Ui\Component\Listing\Column\Store\Options</item>
    <item name="multiple" xsi:type="boolean">true</item>
</item>

But, this is not appearing in the frontend.


Desired Result

When a custom field is configured via layout xml file, it should be appeared in the form.


Technical Details

The problem happens because of the logic present in the method \Loki\AdminComponents\Form\FieldResolver::resolve(). Here it considers only the table columns and not the field definitions provided. If we construct the fields from the combined result of table columns and field defintions, then it will work. Below code works:

    public function resolve(FormRepository $formRepository, array $fieldDefinitions): array
    {
        $resourceModel = $formRepository->getResourceModel();
        if (!$resourceModel) {
            return [];
        }

        $fields = [];
        $tableColumns = $resourceModel->getConnection()->describeTable($resourceModel->getMainTable());
        $tableColumnNames = array_keys($tableColumns);
        $extraFieldDefinitions = array_filter(
            $fieldDefinitions,
            static fn (array $fieldDefinition, string $fieldCode): bool => !in_array($fieldCode, $tableColumnNames),
            ARRAY_FILTER_USE_BOTH
        );
        $fieldColumns = array_merge($tableColumns, $extraFieldDefinitions);
        foreach ($fieldColumns as $fieldCode => $fieldDefinition) {
            $columnData = ['code' => $fieldCode];
            if (in_array($fieldCode, $tableColumnNames)) {
                $tableColumn = $tableColumns[$fieldCode];
                $columnName = $tableColumn['COLUMN_NAME'];
                $fieldType = $this->getFieldTypeCodeFromColumn($formRepository, $tableColumn);
                if ($columnName === $resourceModel->getIdFieldName()) {
                    $fieldType = 'view';
                }

                $fieldLabel = $this->getLabelByColumn($tableColumn['COLUMN_NAME']);

                if (empty($fieldType)) {
                    $fieldType = 'input';
                }

                $columnData = array_merge($columnData, [
                    'field_type' => $fieldType,
                    'label' => $fieldLabel,
                    'required' => false,
                    'sort_order' => 0,
                    'field_attributes' => [],
                    'label_attributes' => [],
                ]);
            }

            if (array_key_exists($fieldCode, $fieldDefinitions)) {
                $columnData = array_merge($columnData, $fieldDefinitions[$fieldCode]);
            }

            $fields[$fieldCode] = $this->fieldFactory->create(
                $this->layout->createBlock(Template::class),
                $columnData,
            );
        }

        uasort($fields, function (Field $field1, Field $field2) {
            return $field1->getSortOrder() <=> $field2->getSortOrder();
        });

        return $fields;
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions