Zend certified PHP/Magento developer

Custom Customer Attribute field data not showing in Customer Account Information

I have added the field “Favorite Color” to the Customer entity and added it to the registration form. The field properly stores data, as I can see when logging in as Admin and checking in Customers -> All Customers -> Edit -> Account Information.

However, when the Customer logged in and goes to the Account Information section, that field is empty. The module.xml file does of course list a dependency on Magento_Customer.

I’ve added the field with this UpgradeData:

public function upgrade(ModuleDataSetupInterface $setup)
{
    $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
    $customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
    $attSetId = $customerEntity->getDefaultAttributeSetId();

    /** @var $attributeSet AttributeSet */
    $attributeSet = $this->attributeSetFactory->create();
    $attGroupId = $attributeSet->getDefaultGroupId($attSetId);

    $customerSetup->addAttribute(Customer::ENTITY, $attribute['code'], [
        'label' => 'Favorite Color',
        'type' => 'varchar',
        'input' => 'text',
        'required' => false,
        'visible' => true,
        'user_defined' => true,
        'position' =>999,
        'system' => 0,
    ]);

    $attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, $attribute['code'])->addData([
        'attribute_set_id' => $attSetId,
        'attribute_group_id' => $attGroupId,
        'used_in_forms' => ['adminhtml_checkout', 'adminhtml_customer', 'adminhtml_customer_address', 'customer_account_create', 'customer_account_edit', 'customer_address_edit', 'customer_register_address'],
    ]);

    $attribute->save();
}

I use the following layout to add a template to the account creation form:

<?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="MagentoFrameworkViewElementTemplate" name="dotan_customer_form_additional_info" template="Dotan_Customer::form/customer_account_create_additional_info.phtml">
            </block>
        </referenceContainer>
    </body>
</page>

And a near-identical layout to add a template to the account edit form:

<?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="form.additional.info">
            <block class="MagentoFrameworkViewElementTemplate" name="dotan_customer_form_edit_additional_info" template="Dotan_Customer::form/customer_account_edit_additional_info.phtml">
            </block>
        </referenceContainer>
    </body>
</page>

Here is the template added to the account creation form:

<div class="field favorite_color">
    <label for="favorite_color" class="label"><span><?php /* @escapeNotVerified */ echo __('Favorite Color') ?></span></label>
    <div class="control">
        <input type="text" name="favorite_color" id="favorite_color" title="Favorite Color" value="" class="input-text" type="text" />
    </div>
</div>

And the identical template added to the account edit form:

<div class="field favorite_color">
    <label for="favorite_color" class="label"><span><?php /* @escapeNotVerified */ echo __('Favorite Color') ?></span></label>
    <div class="control">
        <input type="text" name="favorite_color" id="favorite_color" title="Favorite Color" value="" class="input-text" type="text" />
    </div>
</div>

I suspect that I need to add the fields’ content to the account edit form template. But does that need to be done manually, via e.g. a block that fetches the values from the database?

I’ve looked at many tutorials online but none of them mention the need for such a block. And the information is being provided in the Adminhtml area’s Customer -> Account Information, so I suspect that there is some built-in functionality to provide that information to the Customer’s own Account Information page as well.