3

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

So far I have the following in my new module folder:

Setup/InstallData.php

<?php
        namespace tivol\registrationExtra\Setup;
        use Magento\Framework\Module\Setup\Migration;
        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",  array(
                "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>

Noting shows up in the frontend and backend says "page isn't working"

Any ideas as to what I have done wrong?

2
  • Which is the message? Or only "page isn't working message? Aug 24, 2016 at 16:21
  • Page isn't working is the mrssage
    – Mark
    Aug 24, 2016 at 17:16

1 Answer 1

1

Page not working or Page not found error due ACL missing in your module. Check Acl for custom module.

InstallData.php : add custom text field in backend.

<?php 
              namespace Paarth\CustomerAttribute\Setup;
              use Magento\Framework\Setup\InstallDataInterface;
              use Magento\Framework\Setup\ModuleContextInterface;
              use Magento\Framework\Setup\ModuleDataSetupInterface;

              class InstallData implements InstallDataInterface
              {
                private $customerSetupFactory;


                        public function __construct(\Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory)
                        {
                          $this->customerSetupFactory = $customerSetupFactory;
                        }

                        public function install(ModuleDataSetupInterface
                        $setup, ModuleContextInterface $context)
                        {
                        /** @var CustomerSetup $customerSetup */

                        $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
                        $setup->startSetup();

                         $attributeCode = "custom_text";

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


                        $customerSetup->addAttribute('customer',
                        'custom_text', [
                        'label' => 'Custom Text',
                        'type' => 'text',
                        'frontend_input' => 'text',
                        'required' => false,
                        'visible' => true,
                         'system'=> 0,
                        'position' => 105,
                        ]);

                $loyaltyAttribute = $customerSetup->getEavConfig()->getAttribute('customer', 'custom_text');
                $loyaltyAttribute->setData('used_in_forms',['adminhtml_customer']);
                $loyaltyAttribute->save();

            $setup->endSetup(); }} 
?>

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.