1

I'm tryng to create a custom category attribute using this guide but it doesn't work for me, this is the code that I'm using for the InstallData.php:

use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\InstallDataInterface;

use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;

class InstallData implements InstallDataInterface
{
  private $eavSetupFactory;

  public function __construct(EavSetupFactory $eavSetupFactory) {
    $this->eavSetupFactory = $eavSetupFactory;
  }

  public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
  {
    $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
    $eavSetup->addAttribute(\Magento\Catalog\Model\Category::ENTITY, 'disposizione', [
        'type'     => 'int',
        'label'    => 'Disposizione',
        'input'    => 'boolean',
        'source'   => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
        'visible'  => true,
        'default'  => '0',
        'required' => false,
        'global'   => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
        'group'    => 'Display Settings',
    ]);
  }
}

category_form.xml

<?xml version="1.0"?>
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
<fieldset name="display_settings">
    <field name="disposizione">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="dataType" xsi:type="string">boolean</item>
                <item name="formElement" xsi:type="string">checkbox</item>
                <item name="label" xsi:type="string" translate="true">Nome Produttore</item>
                <item name="prefer" xsi:type="string">toggle</item>
                <item name="valueMap" xsi:type="array">
                    <item name="true" xsi:type="string">1</item>
                    <item name="false" xsi:type="string">0</item>
                </item>
                <item name="default" xsi:type="number">0</item>
            </item>
        </argument>
    </field>
</fieldset>
</form>

module.xml

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
  <module name="Ng_AddAttribute" setup_version="1.0.0" />
</config>

registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
  \Magento\Framework\Component\ComponentRegistrar::MODULE,
  'Ng_AddAttribute',
  __DIR__
);

Schermata_2018-08-07_alle_18.48.29.png

After that i upgrade the schema with bin/magento setup:upgrade and clear the cache, but a error appears when i reload the page: Exception #0 (Exception): Warning: DOMDocument::loadXML(): XML declaration allowed only at the start of the document in Entity this is only a try, I need to create two attribute one text and one select. could someone help me

EDIT:

I've resolved the XML problem and now i can see the field in the backend but it didn't save the data.

2
  • Please check this, are you wanted like below picture. prnt.sc/kfuq3a Aug 7, 2018 at 17:36
  • Yes, first I need a text field for an additional name and than I need to create a select field with 2 options.
    – doy
    Aug 8, 2018 at 7:23

1 Answer 1

1

seems like there is something wrong with XML or setup version in your files.

Your Xml file should start from line one.

run below command

php bin/magento module:enable --clear-static-content Ng_AddAttribute
php bin/magento cache:flush
1
  • Ok, I've resolved the XML problem and now i can see the field in the backend but it didn't save the data.
    – doy
    Aug 8, 2018 at 8:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.