0

I am trying to create a few custom customer attributes and make them usable for the customers in the frontend.

I have created a module that creates the attributes and places these attributes in the adminhtml. In the backend works like a charm.

But i want the customers to be able to insert information in these attributes theirselves.

The location i am trying to place these attributes in is in the customer/account/edit page.

This is part of the code im using in the setup/installdata.php

$customerSetup->addAttribute(Customer::ENTITY, 'child1_firstname', [
    'type' => 'varchar',
    'label' => 'Child 1 firstname',
    'input' => 'text',
    'required' => false,
    'visible' => true,
    'user_defined' => true,
    'sort_order' => 1000,
    'position' => 1000,
    'system' => 0,
]);
$attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'child1_firstname')
->addData([
    'attribute_set_id' => $attributeSetId,
    'attribute_group_id' => $attributeGroupId,
    'used_in_forms' => ['customer_account_create','adminhtml_customer', 'customer_form_edit', 'customer_edit'],
]);
$attribute->save();

And for the xml i created the customer_account_edit.xml And inserted the form/edit block here so i could rebuild this block but with the extra

<referenceContainer name="content">
    <block class="Magento\Customer\Block\Form\Edit" name="customer_edit" template="Sprite_CustomerAttribute::form/edit.phtml" cacheable="false">
        <container name="form.additional.info" as="form_additional_info"/>
    </block>
</referenceContainer>

The next part is the part i am stuck. I am not sure how to go on from here. I have copied the core file to here and then i added the extra fields i want to use here. But i have absolutely no clue on how i can insert the data from the database in these fields. And how i can save them to the database using the same button.

<div class="field child1_firstname">
    <label for="child1_firstname" class="label"><span><?php /* @escapeNotVerified */ echo __('Child 1 firstname') ?></span></label>
    <div class="control">

        <input type="text" 
            name="child1_firstname"
            id="child1_firstname" 
            value="<?php echo 'need information from the custom attribute here' ?>"
            title="<?php /* @escapeNotVerified */ echo __('Child 1 firstname') ?>" 
            class="input-text" 
            data-validate="{required:true}" 
            autocomplete="off">
    </div>
</div>

I did notice that the core is using block php and another phtml file. But im not sure if i have to go the same way for these custom attributes.

Could somebody help me clear up on how i can resolve this issue and get the information of the custom attribute to show and to save it when the customer changes it?

4

This is working for me:

InstallData.php

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->removeAttribute(\Magento\Customer\Model\Customer::ENTITY, "child1_firstname");

            $customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, "child1_firstname",  array(
                "type"     => "varchar",
                "backend"  => "",
                "label"    => "child1_firstname",
                "input"    => "image",
                "source"   => "",
                "visible"  => true,
                "required" => false,
                "default" => "",
                "frontend" => "",
                "unique"     => false,
                "note"       => ""

            ));

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

            $child1_firstname = $customerSetup->getEavConfig()->getAttribute(\Magento\Customer\Model\Customer::ENTITY, 'child1_firstname');
            $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";
            $child1_firstname->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);

            $child1_firstname->save();
            $installer->endSetup();
    }

customer_account_edit.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="content">
         <referenceContainer name="form.additional.info">
            <block class="Magento\Customer\Block\Form\Edit"  template="Sprite_CustomerAttribute::additionalinfocustomer.phtml"/>
         </referenceContainer>
        </referenceContainer>
    </body>
</page>

additionalinfocustomer.phtml

<fieldset class="fieldset create account" >
    <legend class="legend"><span><?php /* @escapeNotVerified */ echo __('Additional Information') ?></span></legend><br>

    <div class="field profile_pic">
        <label for="profile_pic" class="label"><span><?php /* @escapeNotVerified */ echo __('Profile Pic') ?></span></label>
        <div class="control">
            <input type="file" name="child1_firstname" id="child1_firstname" title="<?php /* @escapeNotVerified */ echo __('Children') ?>" class="input-text" autocomplete="off" value="<?php $block->getCustomer()->getChild1Firstname() ?>">
        </div>
    </div>
</fieldset>
|improve this answer|||||
  • Thank you for your answer, I have tried it using your code, however for me it didnt do anything. The fields that were visible on the account edit page are completely gone when i try to use your solution. – Sprite May 1 '17 at 15:14
  • Updated answer..please check now – Sarfaraz bheda May 2 '17 at 4:58
  • Thank you, it worked now. The information (image) i saved in the edit now actually saved it to the attribute. Now i can finally continue on how i want this exactly. – Sprite May 2 '17 at 12:43
  • Just one more question, This also works for the text fields. However in the backend it does load the information right in the textbox. But not in the frontend for the customer. Do you have any idea how i can show the information in the same textbox i used to save it? – Sprite May 2 '17 at 12:52
  • Updated as per your requirement – Sarfaraz bheda May 2 '17 at 13:14

Your Answer

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

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