2

I'm trying to add another field to the customer create form.

The following code troughs an error in the backend. The error is:

Class Magento\Customer\Model\Metadata\Form\String does not exist

or if I refresh the page it will change to:

The requested component ("string") is not found. Before using, you must add the implementation.

and back to the original error if i refresh again.

Setup/InstallData.php

<?php
namespace tivol\registrationExtra\Setup;

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

class InstallData implements InstallDataInterface
{
    /**
     * Customer setup factory
     *
     * @var \Magento\Customer\Setup\CustomerSetupFactory
     */
    private $customerSetupFactory;

    /**
     * Init
     *
     * @param \Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory
     */
    public function __construct(\Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory)
    {
        $this->customerSetupFactory = $customerSetupFactory;
    }

    /**
     * Installs DB schema for a module
     *
     * @param ModuleDataSetupInterface $setup
     * @param ModuleContextInterface $context
     * @return void
     */
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $installer = $setup;
        $installer->startSetup();
        $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
        $entityTypeId = $customerSetup->getEntityTypeId(\Magento\Customer\Model\Customer::ENTITY);

        $customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, "salesAssociate", [
            "type" => "varchar",
            "backend" => "",
            "label" => "Sales associate",
            "input" => "string",
            "source" => 'Magento\Eav\Model\Entity\Attribute\Source\AbstractSource',
            "visible" => true,
            "required" => false,
            "default" => "",
            "frontend" => "",
            "unique" => false,
            "note" => "",

        ]);

        $salesAssociate = $customerSetup->getAttribute(\Magento\Customer\Model\Customer::ENTITY, "salesAssociate");

        $salesAssociate = $customerSetup->getEavConfig()->getAttribute(\Magento\Customer\Model\Customer::ENTITY, 'salesAssociate');
        $used_in_forms[] = "adminhtml_customer";
        $used_in_forms[] = "checkout_register";
        $used_in_forms[] = "customer_account_create";
        /* $used_in_forms[]="customer_account_edit"; */
        $used_in_forms[] = "adminhtml_checkout";
        $salesAssociate->setData("used_in_forms", $used_in_forms)
            ->setData("is_used_for_customer_segment", true)
            ->setData("is_system", 0)
            ->setData("is_user_defined", 1)
            ->setData("is_visible", 1)
            ->setData("sort_order", 100);
        $salesAssociate->save();

        $installer->endSetup();
    }
}

view/frontend/layout/customer_registration.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="form.additional.info">
            <block class="Magento\Framework\View\Element\Template" name="sales_associate" template="MyModules_CustomerMarketingFields::salesassociate.phtml"/>
        </referenceContainer>
    </body>
</page>

view/frontend/templates/salesassociate.phtml

<legend class="legend"><span><?php /* @escapeNotVerified */
        echo __('If you shop in-store who is your regular sales associate') ?></span>
</legend>


<div class="field">
    <label for="associate" class="label"><span><?php /* @escapeNotVerified */
            echo __('Associate') ?></span></label>
    <div class="control">
        <select name="regulation" id="associate" title="<?php /* @escapeNotVerified */
        echo __('Associate') ?>">
            <option value="Option 1">Option 1</option>
            <option value="Option 2">Option 2</option>
            <option value="Option 3">Option 3</option>
        </select>
    </div>
</div>

registration.php

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

etc/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="tivol_registrationExtra" setup_version="0.0.1"/>
</config>

Any ideas as to what I have done wrong?

Siarhey Uchukhlebau
  • 15,957
  • 11
  • 54
  • 83
Mark
  • 755
  • 2
  • 16
  • 33

1 Answers1

4

Try to change this line:

"input"    => "string",

to the:

"input"    => "text",

because a component with name 'string' not exist.

Then reinstall your module.

Note: if on uninstall process your module does not delete the attribute salesAssociate programmatically you should do-it-yourself. Useful link - Magento 2 How to uninstall attributes added by custom module?

Edit:

You can change input type right in the eav_attribute table, just find your attribute by the attribute_code (it should be salesAssociate) and set its frontend_input value as text. I didn't check this solution, but it should help.

Update:

You can just update your install class and reinstall the module. Add the line:

$customerSetup->removeAttribute(\Magento\Customer\Model\Customer::ENTITY, "salesAssociate");

Before the:

$customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, "salesAssociate", [ ....

Remove the record about your module from the setup_module table and execute command bin/magento setup:upgrade in the magento root folder.

Siarhey Uchukhlebau
  • 15,957
  • 11
  • 54
  • 83