2

When i try to add new fields to product creationg form in Magento 2 the html name attribute shows up empty,enter image description here I tried more or less everything and by everything i mean:

1) Adding the custom field i need by adding it to the field pool by using a modifier file

2) Adding the custom field i need by extending XML layout file product_form.xml

3) Adding the custom field i need by making a fieldset in the extending XML layout and then filling fields in there using the FieldFactory class

But no matter which of the above methods i use i end up with the same result of name attribute of being empty is this maybe a bug in the magento2 system?

Here is my code:

1)

AddFieldsData

use Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\AbstractModifier;
use Vendor\Namespace\Ui\Component\Listing\Page\Options as SelectOptions;

class AddFillFieldsData extends AbstractModifier
{
public function __construct(
    SelectOptions $SelectOptions
    )
{
    $this->selectOptions = $SelectOptions;
}
public function modifyMeta(array $meta)
{
    $meta['fields'] = [
        'arguments' => [
            'data' => [
                'config' => [
                    'label' => __('fields'),
                    'sortOrder' => 300,
                    'collapsible' => true,
                    'componentType' => 'fieldset'
                ]
            ]
        ],
        'children' => [
            'new_field' => [
                'arguments' => [
                    'data' => [
                        'config' => [
                            'formElement' => 'select',
                            'componentType' => 'field',
                            'options' => $this->selectOptions->toOptionArray(),
                            'visible' => 1,
                            'dataScope' => "new_field",
                            'required' => 0,
                            'label' => __('Tilføj nyt felt')
                        ]
                    ]
                ]
            ]
        ]
    ];

    return $meta;
}

/**
 * {@inheritdoc}
 */
public function modifyData(array $data)
{
    return $data;
}
}

di.xml enter image description here

2) product_form.xml enter image description here

3) AddFieldData class AddFillFieldsData extends BaseFieldset { /** * @var FieldFactory */ private $fieldFactory;

 public function __construct(
    ContextInterface $context,
    array $components = [],
    array $data = [],
    FieldFactory $fieldFactory,
    SelectOptions $SelectOptions
    )
 {
    parent::__construct($context, $components, $data);
    $this->fieldFactory = $fieldFactory;
    $this->selectOptions = $SelectOptions;
 }

/**
 * Get components
 *
 * @return UiComponentInterface[]
 */
public function getChildComponents()
{
        $fields = [];

        $fields[] = 
        [
            'label' => "Tilføj nyt felt",
            'value' => "",
            'dataType' => "string",
            'dataScope' => "new_field",
            'name' => "new_field",
            'formElement' => "select",
            'options' => $this->selectOptions->toOptionArray(),
        ];



        foreach ($fields as $k => $fieldConfig) {

            $fieldInstance = $this->fieldFactory->create();
            $name = "new_field";

            $fieldData =
            [
                'config' => $fieldConfig,
                'name' => $name
            ];

            $fieldInstance->setData($fieldData);

            $fieldInstance->prepare();
            $this->addComponent($name, $fieldInstance);
        }
        return parent::getChildComponents();


}
}
  • Did you find a solution to this problem? – Silvan Oct 4 '17 at 15:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.